<!DOCTYPE HTML>
<html lang="Chinese">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="javaScript基础, 博客">
    <meta name="description" content="In me the tiger sniffs the rose.">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>javaScript基础 | 凡诚</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.2"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/comment_bg.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">凡诚</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>主页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>文章</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于我</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/about">
          
          <i class="fas fa-user-circle" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>我的信息</span>
        </a>
      </li>
      
      <li>
        <a href="/contact">
          
          <i class="fas fa-comments" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>留言板</span>
        </a>
      </li>
      
      <li>
        <a href="/friends">
          
          <i class="fas fa-address-book" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>友链</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="Search" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/comment_bg.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">凡诚</div>
        <div class="logo-desc">
            
            In me the tiger sniffs the rose.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			主页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			文章
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于我
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-user-circle" style="position: absolute;left:50px" ></i>
			      
		          <span>我的信息</span>
                  </a>
                </li>
              
                <li>

                  <a href="/contact " style="margin-left:75px">
				  
				   <i class="fa fas fa-comments" style="position: absolute;left:50px" ></i>
			      
		          <span>留言板</span>
                  </a>
                </li>
              
                <li>

                  <a href="/friends " style="margin-left:75px">
				  
				   <i class="fa fas fa-address-book" style="position: absolute;left:50px" ></i>
			      
		          <span>友链</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/1.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">javaScript基础</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/html/">
                                <span class="chip bg-color">html</span>
                            </a>
                        
                            <a href="/tags/javaScript/">
                                <span class="chip bg-color">javaScript</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF/" class="post-category">
                                前端技术
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>Publish Date:&nbsp;&nbsp;
                    2021-11-09
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>Update Date:&nbsp;&nbsp;
                    2022-11-10
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>Word Count:&nbsp;&nbsp;
                    32.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>Read Times:&nbsp;&nbsp;
                    128 Min
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>Read Count:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="js基础"><a href="#js基础" class="headerlink" title="js基础"></a>js基础</h2><p>特点：</p>
<ul>
<li>镶嵌在html中。</li>
<li>环境支持。</li>
<li>弱语言类型。</li>
<li>基于对象。</li>
<li>事件驱动。</li>
<li>跨平台。</li>
<li>解释执行。</li>
</ul>
<h4 id="javascript的组成"><a href="#javascript的组成" class="headerlink" title="javascript的组成:"></a>javascript的组成:</h4><ol>
<li>ECMAscript      javascript语法</li>
<li>DOM 页面文档对象</li>
<li>BOM 浏览器对象</li>
</ol>
<h3 id="1-书写位置"><a href="#1-书写位置" class="headerlink" title="1 书写位置"></a>1 书写位置</h3><blockquote>
<p>内嵌式： javascript代码可以写在&lt;script&gt;标签中</p>
<p>行内式：javascript代码可以写在标签中的onclick属性中：</p>
<p>外部式：javascript代码可以写在外部的js文件中，通过&lt;script&gt;标签引入。可以在不同的页面中使用，可以利用到浏览器额缓存机制，推荐使用。一旦引入外部js文件，则当前标签内部代码会被浏览器忽略。规范：不在外部引用的script标签中书写代码。</p>
<p>新建js文件 &gt; 书写javascript代码 &gt; 在html的&lt;script&gt;标签中引入js文件路径</p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>javascript命令测试窗口<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
       可以书写javascript语句
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!--&lt;button>标签中的onclick属性中书写javascript语句，注意双引号单引号的转换--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token special-attr"><span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token value javascript language-javascript"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'弹出警告中的内容'</span><span class="token punctuation">)</span></span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>xxx<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span>
    
    
     <span class="token comment">&lt;!--&lt;a>标签中的href属性中书写javascript:再加javascript语句，注意双引号单引号的转换--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>javascript:alert('弹出警告中的内容')<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">></span></span>
    打开中……
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>外部引入：</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>javascript命令测试窗口<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
    
    <span class="token comment">&lt;!--通过\&lt;script>标签引入--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascriipt<span class="token punctuation">"</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>外部javascript文件的路径<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
       不要书写javascript语句
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    打开中……
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h3 id="2-基础语法："><a href="#2-基础语法：" class="headerlink" title="2 基础语法："></a>2 基础语法：</h3><blockquote>
<p>1.//为单行注释， /*  */为多行注释</p>
<p>2.js严格区分大小写。</p>
<p>3.js语句末尾必须添加 ; 分号，如果不添加浏览器会自动添加，但是会浪费资源甚至添加错误。</p>
<p>4.js会忽略空格，换行和缩进，可以通过空格换行和缩进来控制格式。</p>
</blockquote>
<h3 id="3-基础输出语句："><a href="#3-基础输出语句：" class="headerlink" title="3 基础输出语句："></a>3 基础输出语句：</h3><blockquote>
<p>所有javascript代码写在&lt;script&gt;标签中</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"弹出警告中的内容"</span><span class="token punctuation">)</span>
document<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"打印在页面上的内容"</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"打印在控制台上的内容"</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token string">"计时器名"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//开启计时器</span>
console<span class="token punctuation">.</span><span class="token function">timeEnd</span><span class="token punctuation">(</span><span class="token string">"计时器名"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//关闭计时器，计时器的时间会显示在控制台</span>
console<span class="token punctuation">.</span><span class="token function">dir</span><span class="token punctuation">(</span>对象名<span class="token punctuation">)</span>； <span class="token comment">//可以打印返回的元素，更好的查看里面的属性</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h3 id="基础输入语句"><a href="#基础输入语句" class="headerlink" title="基础输入语句"></a>基础输入语句</h3><blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> print <span class="token operator">=</span> <span class="token function">prompt</span><span class="token punctuation">(</span><span class="token string">'输入内容'</span><span class="token punctuation">)</span>  <span class="token comment">//弹出一个输入框</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

</blockquote>
<h3 id="4-字面量与变量"><a href="#4-字面量与变量" class="headerlink" title="4 字面量与变量"></a>4 字面量与变量</h3><blockquote>
<p>字面量：都是一些不可改变的值，可以直接使用：</p>
<p>1 111 222 333 </p>
<p>变量：变量就是内存中申请的空间。变量可以保存字面量，变量可以任意改变存储的值，还可以通过变量名对字面量进行描述，使用变量之前必须定义：</p>
<p>var 变量名；使用var 声明的变量会在所有语句执行之前率先声明。</p>
<p>变量分为全局变量和局部变量。</p>
</blockquote>
<p>变量的特殊情况：</p>
<table>
<thead>
<tr>
<th>情况</th>
<th>说明</th>
<th>结果</th>
</tr>
</thead>
<tbody><tr>
<td>var age; console.log(age)</td>
<td>只声明不赋值</td>
<td>undefined</td>
</tr>
<tr>
<td>console.log(age)</td>
<td>不声明，不赋值直接使用</td>
<td>报错</td>
</tr>
<tr>
<td>age = 10; console.log(age)</td>
<td>不声明，只赋值</td>
<td>10</td>
</tr>
</tbody></table>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//字面量举例：</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//变量举例：</span>
<span class="token keyword">var</span> a <span class="token operator">=</span><span class="token number">123</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h3 id="5-标识符"><a href="#5-标识符" class="headerlink" title="5 标识符"></a>5 标识符</h3><p>-在js中所有的可以由我们自主命名的都可以称为标识符：变量名，函数名，属性名</p>
<p>-标识符可以含有：字母，_ 下划线，$，数字，不能以数字开头。</p>
<p>-不能是ES中的关键字和保留字。（尽量不使用name）</p>
<p>-一般遵循驼峰命名法。</p>
<p>-底层遵循Unicode编码。</p>
<h3 id="6-数据类型"><a href="#6-数据类型" class="headerlink" title="6 数据类型"></a>6 数据类型</h3><p>JS中的数据类型一共有6种：</p>
<p>基础数据类型：</p>
<p>​            String 字符串</p>
<p>​            Number 数值</p>
<p>​            Boolean 布尔值</p>
<p>​            Null 空值</p>
<p>​            Undefind 未定义</p>
<p>引用数据类型：Object 对象</p>
<p>可以使用typeof函数检查对应数据的数据类型：</p>
<p>console.log(typeof 变量名） 将会在控制台打印响应数据的类型。</p>
<pre class="line-numbers language-javas" data-language="javas"><code class="language-javas">console.log(typeof 变量名）<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h4 id="String"><a href="#String" class="headerlink" title="String"></a>String</h4><blockquote>
<p>可以使用加号连接。</p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
	<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span><span class="token constant">JS</span>中字符串用单引号或者双引号引起来<span class="token operator">--</span><span class="token operator">></span>
	<span class="token keyword">var</span> str<span class="token operator">=</span><span class="token string">"字符串"</span>
    <span class="token keyword">var</span> str1<span class="token operator">=</span><span class="token string">'字符串'</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="字符串-length"><a href="#字符串-length" class="headerlink" title="字符串.length"></a>字符串.length</h5><blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">字符串<span class="token punctuation">.</span>length<span class="token punctuation">;</span>    用于得到字符串的长度<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>


</blockquote>
<p>可以通过转义字符进行特殊字符的输出：</p>
<table>
<thead>
<tr>
<th>字符</th>
<th>转义字符</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>\‘</td>
<td>‘</td>
<td></td>
</tr>
<tr>
<td>\“</td>
<td>“</td>
<td></td>
</tr>
<tr>
<td>\\</td>
<td>\</td>
<td></td>
</tr>
<tr>
<td>\n</td>
<td>换行</td>
<td></td>
</tr>
<tr>
<td>\t</td>
<td>水平tap</td>
<td></td>
</tr>
<tr>
<td>\r</td>
<td>回车</td>
<td></td>
</tr>
<tr>
<td>\v</td>
<td>垂直tap</td>
<td></td>
</tr>
</tbody></table>
<hr>
<h4 id="Number"><a href="#Number" class="headerlink" title="Number"></a>Number</h4><blockquote>
<p>Number 表示数字和浮点数字。</p>
<p>Number的最大值：</p>
<p>Number.MAX_VALUE约是:1.79 *e+308</p>
<p>Number.MIN_VALUE约是：5e-324</p>
<p>如果使用number表示的数字大于最大值则会返回一个</p>
<p>​    Infinity 表示正无穷；</p>
<p>​    -Infinity 表示负无穷；</p>
<p>使用typeof函数检查Infinity会返回Number类型</p>
<p>NAN是一个特殊的数字，表示NOt a Number,使用typeof检查NaN也会返回Number类型。</p>
<p>如果使用js进行浮点数运算可能会得到一个不精确的数值。</p>
</blockquote>
<h5 id="isNaN"><a href="#isNaN" class="headerlink" title="isNaN()"></a>isNaN()</h5><blockquote>
<p>isNaN()用于判断一个数值是不是数字类型</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> <span class="token function">isNaN</span><span class="token punctuation">(</span>判断类型<span class="token punctuation">)</span> <span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<hr>
<h4 id="boolean"><a href="#boolean" class="headerlink" title="boolean"></a>boolean</h4><blockquote>
<p>布尔值只有两种：falst  表示假</p>
<p>​              true   表示真</p>
<p>使用typeof检查返回一个小写的boolear</p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">var</span> bool <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">//定义boolear类型的变量</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<hr>
<h4 id="Null"><a href="#Null" class="headerlink" title="Null"></a>Null</h4><blockquote>
<p>null(空值)类型的值只有一个：null ，是赋给变量以空值，并不是未赋值。</p>
<p>使用typeof检测null类型不会返回null，会返回object类型，<code>null就是定义了一个空的object类。</code></p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">var</span> n <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>  <span class="token comment">//定义一个null类型的变量</span>

</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h4 id="Undefined"><a href="#Undefined" class="headerlink" title="Undefined"></a>Undefined</h4><blockquote>
<p>undefined(未赋值)类型：当<code>声明一个变量却未赋值时</code>，这个变量的数据类型就是undefined.</p>
<p>用typeof检测类型时，返回undefined类型。</p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">var</span> un <span class="token punctuation">;</span>  <span class="token comment">//已经定义变量名，却未赋值！！！</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h4><blockquote>
<p>其他数据类型转换为Number  String类型</p>
</blockquote>
<h5 id="其他类型-–-gt-String"><a href="#其他类型-–-gt-String" class="headerlink" title="其他类型 –&gt;  String"></a>其他类型 –&gt;  String</h5><p>方式一： toString()</p>
<ul>
<li>调用被转换数据类型的toString()方法。</li>
<li>该方法不会影响到原变量，它会将转换的结果返回。</li>
<li>null和undefined这两个值没有toString方法，如果调用他们的方法会报错。</li>
</ul>
<p>方式二(  String()  变量  )：</p>
<ul>
<li>对于Number和Boolean实际上就是调用toString()方法。</li>
<li>对于null和undefined就是将类型名转换为字符串。</li>
</ul>
<p>方式三： + 拼接字符串  （隐式转换）</p>
<ul>
<li>其他类型数据  + ‘’；</li>
</ul>
<h5 id="其他类型-–-gt-Number"><a href="#其他类型-–-gt-Number" class="headerlink" title="其他类型 –&gt;  Number"></a>其他类型 –&gt;  Number</h5><table>
<thead>
<tr>
<th>方式</th>
<th>说明</th>
<th>案例</th>
</tr>
</thead>
<tbody><tr>
<td>parseInt(字符串)</td>
<td>将string类型转成整数类型</td>
<td>parsInt(“56”)</td>
</tr>
<tr>
<td>parseFloat(字符串)</td>
<td>将string类型转成浮点数类型</td>
<td>parseFloat(“56”)</td>
</tr>
<tr>
<td>Number()强制转换函数</td>
<td>将string类型转成数值类型</td>
<td>Number(‘12’)</td>
</tr>
<tr>
<td>js隐式转换(- * /)</td>
<td>利用算数运算隐式转换为数值型</td>
<td>‘12’ - 0</td>
</tr>
</tbody></table>
<p>方法一 使用Number()函数：</p>
<ul>
<li><p>字符串 –&gt; 数字</p>
<p>1.如果是纯数字字符串，则直接转换成数字。</p>
<p>2.如果字符串中有非数字的内容，则转换成NaN。</p>
<p>3.如果字符串是一个空串或者是一个全是空格的字符串。 0</p>
</li>
<li><p>布尔 –&gt; 数字 </p>
<p>true 转换成 1</p>
<p>false 转换成 0</p>
</li>
<li><p>null –&gt; 数字   0</p>
</li>
<li><p>undefined –&gt; 数字 NaN</p>
</li>
</ul>
<p>方法二：</p>
<p>​    parseInt(字符串)可以将一个字符串中的有效的整数内容拿出来，然后转换成Number。</p>
<p>​    parseFloat(字符串)可以将一个字符串中的有效的有效小数内容拿出来，然后转换成Number。</p>
<h5 id="其他类型-–-gt-boolean"><a href="#其他类型-–-gt-boolean" class="headerlink" title="其他类型 –&gt; boolean"></a>其他类型 –&gt; boolean</h5><p>使用Boolean()函数：</p>
<p> 数字 –&gt; boolean</p>
<pre><code> 除了0和NaN，其余的都是true。
</code></pre>
<p>字符串 –&gt; boolea</p>
<p>​    除了空串，其余的都是true。</p>
<p>null和undefined都会转换为false</p>
<p>对象也会转换为true。</p>
<h4 id="6-1-简单数据类型和复杂数据类型"><a href="#6-1-简单数据类型和复杂数据类型" class="headerlink" title="6.1 简单数据类型和复杂数据类型"></a>6.1 简单数据类型和复杂数据类型</h4><blockquote>
<p>简单数据类型又被叫做基本数据类型或者值类型,复杂数据类型又叫做引用数据类型.</p>
</blockquote>
<ul>
<li>值类型:简单数据类型/基本数据类型,在存储变量中存储的是值本身,因此又被叫做值类型. (string number boolean undefined null)</li>
<li>引用类型:发复杂数据类型,在存储时变量存储的仅仅是地址(引用)因此被叫做引用数据类型,通过new关键字创建的对象(系统对象,自定义对象),如 Object,Array,Date等.</li>
<li>简单数据类型存放到栈中,由系统自动分配释放存放函数的参数值,局部变量的值等,其操作方式类似于数据结构中的栈.</li>
<li>复杂数据类型存储到堆中,一般由认为分配释放,或者由垃圾回收机制回收.首先在栈中存放地址,十六进制表示,这个地址指向堆中的数据.</li>
</ul>
<h4 id="6-2-简单数据类型和复杂数据类型传参"><a href="#6-2-简单数据类型和复杂数据类型传参" class="headerlink" title="6.2 简单数据类型和复杂数据类型传参"></a>6.2 简单数据类型和复杂数据类型传参</h4><blockquote>
<ol>
<li>简单数据类型向函数中的形参传递参数时,其实是把在栈空间里的值复制了一份给形参,在方法中对形参做任何修改,都不会影响到外部变量.</li>
<li>引用数据类型向函数中的形参传递参数是,其实是把变量在栈空间里保存的堆地址复制给了形参,形参和实参其实保存的是同一个堆地址,所以操作的是同一个对象.</li>
</ol>
</blockquote>
<h3 id="7-运算符"><a href="#7-运算符" class="headerlink" title="7 运算符"></a>7 运算符</h3><blockquote>
<p>运算符又叫操作符。</p>
<p>通过运算符可以对一个或者多个值进行运算，获取运算结果。</p>
</blockquote>
<h4 id="7-1算数运算符"><a href="#7-1算数运算符" class="headerlink" title="7.1算数运算符"></a>7.1算数运算符</h4><table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>加</td>
<td></td>
</tr>
<tr>
<td>-</td>
<td>减</td>
<td></td>
</tr>
<tr>
<td>*</td>
<td>乘</td>
<td></td>
</tr>
<tr>
<td>/</td>
<td>除</td>
<td></td>
</tr>
<tr>
<td>%</td>
<td>取模</td>
<td></td>
</tr>
</tbody></table>
<blockquote>
<p>+ </p>
</blockquote>
<p>​    1.可以对两个值进行加法运算，并将结果返回。如果对两个字符串进行加法运算，则会返回拼串。将两个字符串拼为一个字符串并返回。</p>
<p>​    2.任何值和字符串做加法运算，都会先转换为字符串，然后再和字符串左拼串的操作。</p>
<blockquote>
<p> -  *  / </p>
</blockquote>
<p>​    可以对两个值进行相关的运算，返回相应的值。和非数字类型运算时，会将非数字类型转换为数字类型再进行运算。</p>
<p>大于号，小于号，大于等于，小于等于：</p>
<blockquote>
<p>判断符号左侧的值是否大于，小于，大于等于，小于等于右侧的，成立返回true，不成立返回false。</p>
</blockquote>
<p>非数值情况</p>
<blockquote>
<p>对于数值的比较会将非数值的字符转换为数值再进行比较。</p>
<p>非纯数字字符值转换为数字是NaN，任何数字和NaN比较都是false。</p>
<p>如果比较两边都是字符，则不会转换为数字，直接依次比较编码。</p>
</blockquote>
<h4 id="7-2-一元运算符"><a href="#7-2-一元运算符" class="headerlink" title="7.2 一元运算符"></a>7.2 一元运算符</h4><blockquote>
<p>只需要一个操作数</p>
</blockquote>
<blockquote>
<p> + 正号</p>
</blockquote>
<p>​        正号不会对数字产生影响。</p>
<blockquote>
<p> -  负号</p>
</blockquote>
<p>​        负号可以对数字取反。</p>
<p>​        对于非数字类型会先转换为数字类型，再进行取反。</p>
<h4 id="7-3-自增自减"><a href="#7-3-自增自减" class="headerlink" title="7.3 自增自减"></a>7.3 自增自减</h4><blockquote>
<p> ++ 自增</p>
</blockquote>
<p>​        1.通过自增可以使变量在自身基础上增加一</p>
<p>​        2.对于一个变量自增以后，原变量的值会立刻增加一</p>
<pre><code>    3.自增分为两种：后++    (a++)  前++   (a++)无论是哪一种自增后的值都相同，但是a++返回自增前的值，++a是返回自增后的值。
</code></pre>
<blockquote>
<p> – 自减</p>
</blockquote>
<p>​        1.通过自减可以使变量在自身基础上减少一</p>
<p>​        2.对于一个变量自减以后，原变量的值会立刻减少一</p>
<pre><code>    3.自减分为两种：后减 (a--)  前减 (--a) 无论是哪一种自减少的值都相同，但是  a-- 是自减少前的值，--a 是自减少后的值。
</code></pre>
<h4 id="7-4-逻辑运算符"><a href="#7-4-逻辑运算符" class="headerlink" title="7.4 逻辑运算符"></a>7.4 逻辑运算符</h4><blockquote>
<p>！ 非</p>
</blockquote>
<ol>
<li>可以用来对一个值进行非运算，所谓非运算就是对一个布尔值进行取反操作。</li>
<li>如果对于一个值进行两次取反，这个值不会变化。</li>
<li>如果对于非布尔值进行取反，则会先将非布尔值转化为布尔值再进行取反操作。(原理和Boolean()函数一样)</li>
</ol>
<blockquote>
<p>&amp;&amp; 与</p>
</blockquote>
<ol>
<li><p>可以对符号两侧的值进行与运算并返回结果。</p>
</li>
<li><p>运算规则：</p>
<p>两个值只要有一个是false就返回false，只有两个值都是true时才返回true。</p>
<p>属于短路与，只要第一个判断出false就不会运行第二个判断表达式。</p>
<ul>
<li><p>可以在逻辑与两边放置表达式</p>
</li>
<li><p>```javascript<br>console.log(123 &amp;&amp; 456);    //如果表达式1为真返回第二个表达式的值,如果有多个值返回第一个假或者最后一个值<br>返回： 123<br>console.log(0 &amp;&amp; 456);        //如果表达式1为假真返回第一个表达式的值<br>返回：  456</p>
<pre class="line-numbers language-none"><code class="language-none">
3. 非布尔值进行运算，会先转换为布尔值，再进行运算。

&gt; || 或

1. 可以对符号两侧的值进行或运算并返回结果。

2. 运算规则：

   两个值只要有一个是true就返回true，只有两个值都是false时才返回false。

   属于短路或，只要第一个判断出true就不会运行第二个判断表达式。

   1. - 可以在逻辑与两边放置表达式

      - &#96;&#96;&#96;javascript
        console.log(123 || 456);    &#x2F;&#x2F;如果表达式1为真返回第一个个表达式的值
        返回： 123
        console.log(0 || 456);		&#x2F;&#x2F;如果表达式1为假真返回第二个个表达式的值如果有多个值返回第一个真或者最后一个值
        返回：  456<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
</li>
<li><p>非布尔值进行运算，会先转换为布尔值，再进行运算。</p>
</li>
</ol>
<h4 id="7-5-赋值运算符"><a href="#7-5-赋值运算符" class="headerlink" title="7.5 赋值运算符"></a>7.5 赋值运算符</h4><blockquote>
<p>= </p>
</blockquote>
<p>  可以将符号右侧的值赋值给符号左侧的变量</p>
<blockquote>
<p>+= -= *= /= </p>
</blockquote>
<p>  a+=x   等同于   a = a+x</p>
<h4 id="7-6-关系运算符"><a href="#7-6-关系运算符" class="headerlink" title="7.6 关系运算符"></a>7.6 关系运算符</h4><blockquote>
<p>通过关系成立它立刻会返回true，如果关系不成立则立刻返回false</p>
</blockquote>
<blockquote>
<p>&gt; 大于</p>
</blockquote>
<p>​    比较左侧是否大于右侧，关系成立它立刻会返回true，如果关系不成立则立刻返回false</p>
<blockquote>
<p>&lt; 小于</p>
</blockquote>
<p>​    比较左侧是否小于右侧，关系成立它立刻会返回true，如果关系不成立则立刻返回false</p>
<blockquote>
<p>&lt;= 小于等于</p>
</blockquote>
<p>​    比较左侧是否小于等于右侧，关系成立它立刻会返回true，如果关系不成立则立刻返回false</p>
<blockquote>
<p>&gt;= 大于等于</p>
</blockquote>
<pre><code> 比较左侧是否大于等于右侧，关系成立它立刻会返回true，如果关系不成立则立刻返回false
</code></pre>
<blockquote>
<p>==  等于</p>
</blockquote>
<p>​    比较左侧是否等于右侧，关系成立它立刻会返回true，如果关系不成立则立刻返回false。</p>
<p>如果左右表达式类型不相同，==会转换为相同的格式再比较。==</p>
<p>​    NaN不和任何值相等包括它本身。</p>
<p>​        可以通过 isNaN()函数来检查是否是NaN。</p>
<blockquote>
<p>!= 不等</p>
</blockquote>
<p>​    比较左侧是否不等右侧，关系成立它立刻会返回true，如果关系不成立则立刻返回false。</p>
<blockquote>
<p>=== 全等于</p>
</blockquote>
<p>​    不会做类型转换，必须类型也相同。如果类型不同，直接返回false。</p>
<blockquote>
<p>!== 不全等</p>
</blockquote>
<p>​    不会做类型转换，必须类型也相同。如果类型不同，直接返回true。</p>
<p><code>布尔值比较时：</code></p>
<p>​        先将其他类型转换为布尔类型，在进行比较。</p>
<h4 id="7-7-条件运算符"><a href="#7-7-条件运算符" class="headerlink" title="7.7 条件运算符"></a>7.7 条件运算符</h4><blockquote>
<p>三元运算符</p>
</blockquote>
<p>语法：</p>
<p>   判断语句？语句一：语句二</p>
<p>条件运算符在执行的时候，首先对条件表达式进行求值，如果值为true就执行表达式一，如果为false就执行表达式二。</p>
<h4 id="7-8-运算符优先级"><a href="#7-8-运算符优先级" class="headerlink" title="7.8 运算符优先级"></a>7.8 运算符优先级</h4><ul>
<li>.   []  new</li>
<li>()</li>
<li>++  –</li>
<li>!    ~    +(单目)    -(单目)    typeof    delete</li>
<li>%    *    /</li>
<li>+(双目)     -(双目)</li>
<li>&lt;&lt;    &gt;&gt;    &gt;&gt;&gt;</li>
<li>&lt;    &lt;=    &gt;     &gt;=</li>
<li>==    !==   ===</li>
<li>&amp;</li>
<li>^</li>
<li>|</li>
<li>&amp;&amp;  （短路）</li>
<li>||</li>
<li>?:</li>
<li>=   +=   -=   /=   %=   &lt;&lt;=    &gt;&gt;=   &gt;&gt;&gt;=    &amp;=    ^=    |=</li>
<li>,</li>
</ul>
<h3 id="8-代码块"><a href="#8-代码块" class="headerlink" title="8 代码块"></a>8 代码块</h3><blockquote>
<p>js中使用一对大括号来给一系列代码语句分组。</p>
<p>一个代码块中的语句要么都执行，要么都不执行，代码块只具有分组功能，==不具备区分变量适用范围的作用。==</p>
</blockquote>
<h3 id="9-条件判断语句"><a href="#9-条件判断语句" class="headerlink" title="9 条件判断语句"></a>9 条件判断语句</h3><blockquote>
<p>是流程控制语句的一种，可以通过判断一个表达式的对错来控制流程。</p>
</blockquote>
<h4 id="if语句："><a href="#if语句：" class="headerlink" title="if语句："></a>if语句：</h4><blockquote>
<p>if(条件表达式){                    //先判断if表达式后的条件表达式，如果为true即执行if</p>
<p>​        语句； }                        后的代码块。如果为false即向下判断。</p>
<p>else if(条件表达式){                //先判断if表达式后的条件表达式，如果为true即执行if</p>
<p>​       语句；}                            else后的代码块。如果为false即向下执行。</p>
<p>else {                                        //如果以上全部未判断成功即执行else中的代码块。</p>
<p>​       语句；}</p>
</blockquote>
 <pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">if</span><span class="token punctuation">(</span>条件表达式<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>                    
	<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">&#125;</span>						
<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>条件表达式<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>		
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">&#125;</span>							
<span class="token keyword">else</span> <span class="token punctuation">&#123;</span>								
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h4 id="switch语句"><a href="#switch语句" class="headerlink" title="switch语句:"></a>switch语句:</h4><blockquote>
<p>switch 匹配需要类型和值必须一样</p>
</blockquote>
<p>switch(变量名){                   //先在switch后输入一个需要判断的变量名或者值</p>
<p>​    case 值1:                 //case后给出判断的值如果成功就会执行后面的语句，(全等比较！)</p>
<p>​        判断成功的语句1；            //直到碰到break；语句，如果不碰到将会一直执行。</p>
<p>​        break；                            //如果只想执行一句case语句，必须加break，否则将会继续执行后方的语句。</p>
<p>case 值2:                                    </p>
<p>​        判断成功的语句2；</p>
<p>​        break；</p>
<p>default:</p>
<p>​        只要判断不成功就执行；</p>
<p>…………}</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">switch</span><span class="token punctuation">(</span>变量名<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>                   
	<span class="token keyword">case</span> 值<span class="token number">1</span><span class="token operator">:</span> 										
		<span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">break</span>；							
<span class="token keyword">case</span> 值<span class="token number">2</span><span class="token operator">:</span>									
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">break</span>；
<span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
…………<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="10-循环语句"><a href="#10-循环语句" class="headerlink" title="10 循环语句"></a>10 循环语句</h3><blockquote>
<p>通过循环语句可以实现对一部分语句的循环实现。</p>
</blockquote>
<h4 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h4><p>while(判断语句){   //进入循环之前先判断，如果为true即执行循环体，执行完循环体再进行</p>
<p>​    循环体语句；    //判断语句的判断，如果成功，再进行循环体的执行，如果失败退出循环</p>
<p>}</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">while</span><span class="token punctuation">(</span>Var<span class="token operator">==</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">//如果判断语句中写 true 即这个循环会一直。可以使用break来推出循环。</span>
    循环体语句<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h4 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h4><p>for(初始化语句 ; 条件判断语句 ; 更新表达式){</p>
<p>​    循环体语句l;</p>
<p>}</p>
<p>for(i in 数组名或者属性){   //用来遍历用户定义的属性或者数组。</p>
<p>​    循环体语句l;</p>
<p>}</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">100</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    循环体语句；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h4 id="do-while循环"><a href="#do-while循环" class="headerlink" title="do while循环"></a>do while循环</h4><p>do{                      //先执行一遍循环体再进行条件的判断。</p>
<p>​    循环体语句；</p>
<p>}while(条件判断语句)；</p>
<h4 id="break和continue"><a href="#break和continue" class="headerlink" title="break和continue"></a>break和continue</h4><p>break可以用来退出循环语句和switch语句。</p>
<p>循环嵌套的时候，break只会影响最近的循环，不会影响其他循环。</p>
<p>continue可以结束当前循环，开始下一次的循环。</p>
<p>break：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//可以为循环语句创建标签名，使内层break可以作用于外层循环。</span>
<span class="token literal-property property">labelName</span><span class="token operator">:</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">break</span> labelName<span class="token punctuation">;</span>   <span class="token comment">//break 标签名；的形式可以结束指定循环语句。</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>continue:</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">

<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token keyword">continue</span><span class="token punctuation">;</span>   <span class="token comment">//用于结束当次循环，跳过当次循环，之后继续进行</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>return：</p>
<blockquote>
<p>直接跳出函数，无视下面代码。</p>
</blockquote>
<h3 id="10-数组"><a href="#10-数组" class="headerlink" title="10* 数组"></a>10* 数组</h3><blockquote>
<p>数组： 就是一组数据的集合，存储在单个变量下的优雅方式。</p>
</blockquote>
<p>数组中的元素没有类型限制，可以存储多个类型的数据。</p>
<h4 id="10-1-数组定义："><a href="#10-1-数组定义：" class="headerlink" title="10.1 数组定义："></a>10.1 数组定义：</h4><ol>
<li>利用new创建数组</li>
</ol>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ol start="2">
<li>利用字面量创建数组</li>
</ol>
<blockquote>
<p>var arr = [1,2,3];</p>
</blockquote>
<h4 id="10-2-数组的访问"><a href="#10-2-数组的访问" class="headerlink" title="10.2 数组的访问"></a>10.2 数组的访问</h4><p>通过索引来访问，设置，修改对应的元素。</p>
<p>数组名[索引],数组的索引以0为开始。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>如果访问数组下标超出返回undefined。</p>
<p>遍历：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h4 id="10-3-数组的扩容"><a href="#10-3-数组的扩容" class="headerlink" title="10.3 数组的扩容"></a>10.3 数组的扩容</h4><p>数组可以使用.length属性类实现访问，修改数组的长度</p>
<ul>
<li>数组长度 length属性会自动检测数组的长度。</li>
</ul>
<p>修改数组的长度时，如果增加了数组的长度，未赋值的数组元素为undefined</p>
<p>不能直接给数组名赋值，否则会覆盖所有的数组元素。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">arr<span class="token punctuation">.</span>length；   <span class="token comment">//得到数组的长度</span>
arr<span class="token punctuation">.</span>length <span class="token operator">=</span> <span class="token number">10</span>；  <span class="token comment">//修改数组的长度</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>修改索引号将把指定数组的长度也修改。如果是扩容，为赋值的数组将为undefined。</p>
<p>也可以直接使用数组名[扩充索引]格式直接对扩充索引赋值。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>   <span class="token comment">//直接使用扩充索引修改数组长度。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>







<h3 id="11-对象-自定义"><a href="#11-对象-自定义" class="headerlink" title="11 对象(自定义)"></a>11 对象(自定义)</h3><blockquote>
<p>对象是一个具体的实物。在js中对象是一组无序的相关属性和方法的集合，所有的事物都是对象，例如 字符串，数值，数组，函数。</p>
</blockquote>
<p>对象由属性和方法组成的：</p>
<p>属性：事物的特征，在对象中使用属性来表示。</p>
<p>方法： 事物的行为，在对象中使用方法来表示。</p>
<p>內建对象：</p>
<ul>
<li>在es标准中定义的对象，在任何es实现中都可以使用；比如Math，Number，Boolean，Function，Object</li>
</ul>
<p>宿主对象；</p>
<ul>
<li>由JS的运行环境中提供的对象，主要指浏览器提供的对象。比如BOM DOM。</li>
</ul>
<p>自定义对象：</p>
<ul>
<li>由开发人员自己定义的对象。</li>
</ul>
<h4 id="创建对象："><a href="#创建对象：" class="headerlink" title="创建对象："></a>创建对象：</h4><blockquote>
<p>使用new关键字调用的函数，是构造函数，构造函数是专门用于创建对象的函数。</p>
<p>使用typeof函数检查一个对象时，返回值为object。</p>
<p>还可以通过字面量的形式新建对象。</p>
</blockquote>
<p>在js中由三种方式可以创建对象。</p>
<ul>
<li>利用字面量创建对象。</li>
<li>利用new Object创建对象。</li>
<li>利用构造函数创建对象。</li>
</ul>
<h5 id="使用字面量创建对象"><a href="#使用字面量创建对象" class="headerlink" title="使用字面量创建对象"></a>使用字面量创建对象</h5><blockquote>
<p>对象的字面量是 {} 花括号</p>
<ol>
<li>类中属性或者方法使用键值对的形式： 属性名 ： 属性值，</li>
<li>多个属性或者方法之间使用逗号隔开。</li>
</ol>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
<span class="token literal-property property">uname</span><span class="token operator">:</span> <span class="token string">'name'</span><span class="token punctuation">,</span>
<span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token string">'22'</span><span class="token punctuation">;</span>
<span class="token function-variable function">sayHi</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'打印输出'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="使用new-Object创建对象"><a href="#使用new-Object创建对象" class="headerlink" title="使用new Object创建对象"></a>使用new Object创建对象</h5><blockquote>
<p>使用 var 对象名 = new Object();来创建对象。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//创建了一个空对象</span>
obj<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'objname'</span><span class="token punctuation">;</span>    <span class="token comment">//向空对象中追加属性</span>
obj<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">18</span><span class="token punctuation">;</span>
obj<span class="token punctuation">.</span><span class="token function-variable function">fun</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  方法体；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="使用构造方法创建对象"><a href="#使用构造方法创建对象" class="headerlink" title="使用构造方法创建对象"></a>使用构造方法创建对象</h5><blockquote>
<p>如果要创建多个对象，且这些对象中有很多属性和方法相同，可以利用函数的方法，将重复的代码封装到函数中，这个函数被称为构造函数。</p>
<p>构造函数就是把对象中的一些相同的属性和代码抽象出来封装到函数中。</p>
<p>使用构造函数属性和方法前必须添加this</p>
<p>使用构造函数创建对象的过程称为对象的实例化。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//构造函数规范：</span>
<span class="token keyword">function</span> <span class="token function">构造函数名</span><span class="token punctuation">(</span><span class="token parameter">形参</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>属性名 <span class="token operator">=</span> 形参；
  <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">方法</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//调用构造函数</span>
<span class="token keyword">var</span> 对象名 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">构造函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>new 关键字执行过程：</p>
<ol>
<li>new 构造函数再内存中创建一个空的对象，</li>
<li>this 就会执行创建的对象。</li>
<li>执行构造函数里的代码，为空对象添加属性和方法。</li>
<li>返回这个对象。</li>
</ol>
<h4 id="使用对象"><a href="#使用对象" class="headerlink" title="使用对象"></a>使用对象</h4><p>调用对象的属性采取 对象名.属性名，属性名不能单独调用，只能通过对象调用。</p>
<p>调用对象属性还可以采取 对象名[‘属性名’]</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
  <span class="token literal-property property">uname</span><span class="token operator">:</span> <span class="token string">'name'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token string">'22'</span><span class="token punctuation">;</span>
  <span class="token function-variable function">sayHi</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'打印输出'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span><span class="token string">'age'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>调用对象的方法：  对象名.方法名();只能通过对象调用。不能单独调用。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
  <span class="token literal-property property">uname</span><span class="token operator">:</span> <span class="token string">'name'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token string">'22'</span><span class="token punctuation">;</span>
  <span class="token function-variable function">sayHi</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'打印输出'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
obj<span class="token punctuation">.</span><span class="token function">seyHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="添加属性"><a href="#添加属性" class="headerlink" title="添加属性"></a>添加属性</h4><blockquote>
<p> 在对象中保存的值称为属性。</p>
</blockquote>
<p>对象的属性名不强制遵守变量名的命名规范。但是有特殊的存取格式：对象名[“属性名”] = 属性值；   //该方式可以使用变量值</p>
<p>向对象中添加属性语法：对象名.属性名 = 属性值;</p>
<p>对象名.属性名 = 函数名 ；      //此时调用函数名和调用对象名.属性名是等价的。</p>
<h4 id="读取对象"><a href="#读取对象" class="headerlink" title="读取对象"></a>读取对象</h4><p>读取对象中的属性语法：对象名.属性名；  对象名[属性名];     //可以将变量填入，会转换成字符串。</p>
<p>==如果读取对象中没有的属性不会报错，会返回一个undefined.==</p>
<h4 id="修改对象的属性"><a href="#修改对象的属性" class="headerlink" title="修改对象的属性"></a>修改对象的属性</h4><blockquote>
<p>修改对象的属性值：对象名.属性名 = 新值；</p>
</blockquote>
<h4 id="删除对象的属性"><a href="#删除对象的属性" class="headerlink" title="删除对象的属性"></a>删除对象的属性</h4><blockquote>
<p>删除对象的指定属性：delete 对象名.属性名；</p>
</blockquote>
<h4 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h4><blockquote>
<p>在对象中定义的函数叫做对象的方法，调用方式： 对象名.方法名()</p>
</blockquote>
<h4 id="for-in-循环"><a href="#for-in-循环" class="headerlink" title="for in 循环"></a>for in 循环</h4><blockquote>
<p>for in 循环可以对对象和数组进行遍历操作。</p>
<p>使用for in循环语句，对象中有几个属性，循环体就会执行几次，for in 循环可以遍历方法。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token literal-property property">a</span> <span class="token operator">:</span> <span class="token string">"a"</span><span class="token punctuation">,</span><span class="token literal-property property">b</span> <span class="token operator">:</span> <span class="token string">"b"</span><span class="token punctuation">,</span><span class="token literal-property property">c</span> <span class="token operator">:</span> <span class="token string">"c"</span><span class="token punctuation">&#125;</span>

<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//输出对象中的所有属性名</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//输出对象中的所有属性值</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h4><blockquote>
<p>创建一个构造函数，专门用于创建对象</p>
<p>构造函数就是一个普通的函数，创建方式和普通函数没有区别，不同的只是调用时要加 new 并且首字母大写。</p>
<p>使用同一个构造函数创建的对象称为一类构造对象。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">Dog</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    
<span class="token punctuation">&#125;</span>
<span class="token keyword">var</span> dog <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//构造的是Dog类型的对象</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="对象中方法的两种定义方式"><a href="#对象中方法的两种定义方式" class="headerlink" title="对象中方法的两种定义方式"></a>对象中方法的两种定义方式</h4><blockquote>
<p>1.直接在构造函数中定义方法，每次执行构造函数都会执行其中的构造函数。</p>
<p>this.fun = function(){ 函数体 }</p>
<p>2.在构造函数中只定义this.s = fun，然后在构造函数外定义函数。</p>
<p>function fun(){ 函数体 }  //在全局命名空间中定义，不安全。会污染命名空间。</p>
</blockquote>
<h4 id="instanceof-运算符"><a href="#instanceof-运算符" class="headerlink" title="instanceof 运算符"></a>instanceof 运算符</h4><blockquote>
<p>对象名 instanceof 类名  可以检查一个对象是否是一个类的实例。</p>
</blockquote>
<h4 id="原型对象-prototype"><a href="#原型对象-prototype" class="headerlink" title="原型对象 prototype"></a>原型对象 prototype</h4><blockquote>
<p>我们创建的每一个函数都有一个prototype属性，对应着一个对象，就是<code>原型对象</code>。</p>
<p>普通函数的原型对象没有任何用处，构造函数的原型对象相当于创造了一个类共有的空间。</p>
<p>原型对象相当于一个公共的区域，所有同一个类的实例都可以访问这个原型对象，可以将所有对象中的共有内容设置到原型对象中。</p>
<p>==当访问对象中的属性或者方法时，会现在对象自身中寻找，如果找到就会直接使用。如果找不到就会去类的原型对象中寻找，找到后直接使用。==</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">Cql</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"这是属性"</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>old <span class="token operator">=</span> <span class="token string">"这是属性"</span><span class="token punctuation">;</span>
            <span class="token class-name">Cql</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>s <span class="token operator">=</span> <span class="token string">"这是原型对象中的值"</span>
            <span class="token class-name">Cql</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">a</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"原型对象中的函数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
<span class="token keyword">var</span> cql <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cql</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
cql<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//调用原型对象中的函数。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="in"><a href="#in" class="headerlink" title="in"></a>in</h4><blockquote>
<p>通过in可以判断对象中是否含有相应的属性值，也会查找原型对象中有无相应的属性名。</p>
</blockquote>
<h4 id="hasOwnProperty方法"><a href="#hasOwnProperty方法" class="headerlink" title="hasOwnProperty方法"></a>hasOwnProperty方法</h4><blockquote>
<p>通过 对象名.hasOwnProperty(“属性名”) 方法可以检查这个对象中是否含有对应的属性，但是不会检查原型对象中。</p>
</blockquote>
<h4 id="toString方法"><a href="#toString方法" class="headerlink" title="toString方法"></a>toString方法</h4><blockquote>
<p>每个对象中都有tostring()方法，tostring()方法的返回值就是输出对象名时输出的内容。</p>
<p>toString()方法是在_proto_的_proto_中，只要在类的prototype中重写tostring()方法，就可以使这个类型的每一个对象都使用此toString()方法，只在一个对象中重写tostring()方法，只是在此对象中可以使用。</p>
</blockquote>
<hr>
<h3 id="12-函数"><a href="#12-函数" class="headerlink" title="12 函数"></a>12 函数</h3><p>可以将要封装的代码以字符串的形式传递给构造函数</p>
<p>构造函数语法：var 函数名 = new Function(“函数代码”);</p>
<p>函数定义之前加 var 可以在任意位置调用。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//创建函数</span>
<span class="token keyword">var</span> 函数名 <span class="token operator">=</span> <span class="token function">Function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token string">"console.log("</span>输出语句<span class="token string">")"</span><span class="token punctuation">&#125;</span>

funtion <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token parameter">形参</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"输出语句"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//调用函数,如果有形参，可以指定对应的实参。</span>
<span class="token function">函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="12-1-函数参数"><a href="#12-1-函数参数" class="headerlink" title="12.1 函数参数"></a>12.1 函数参数</h4><blockquote>
<p>声明函数语法：Function 函数名(形参，形参){ 函数代码 }</p>
<p>但是函数不会检查实参的类型，可能会收到不合法的数据。(最好进行参数的检查）</p>
<p>==函数也不会检查参数的数量，多余的参数将不会赋值。==</p>
<p>封装的函数不会立刻执行。需要调用 函数名() 之后才会执行。</p>
<p>形参可以是变量，也可以是对象(函数也是对象).</p>
<p>函数可以嵌套使用。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//创建函数</span>
<span class="token keyword">var</span> 函数名 <span class="token operator">=</span> <span class="token function">Function</span><span class="token punctuation">(</span>实参<span class="token punctuation">)</span><span class="token punctuation">;</span>

funtion <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token parameter">形参</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"输出语句"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//调用函数,如果有形参，可以指定对应的实参。</span>
<span class="token function">函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">*</span>
    <span class="token function">函数名</span><span class="token punctuation">(</span>函数名<span class="token punctuation">)</span>；     <span class="token comment">//调用函数，并且传入一个对象，这个对象也是个函数</span>

	<span class="token function">函数名</span><span class="token punctuation">(</span><span class="token function">函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>		<span class="token comment">//调用函数，并且将另一个函数的返回值作为实参。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="12-2-函数返回"><a href="#12-2-函数返回" class="headerlink" title="12.2 函数返回"></a>12.2 函数返回</h4><blockquote>
<p>在函数的最后使用return来返回一个值，在调用函数时用一个变量接受。</p>
<p>函数中，==在return 后的代码都不会执行。==</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">funtion <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token parameter">形参</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">return</span> 返回值<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">var</span> 变量名 <span class="token operator">=</span> <span class="token function">函数名</span><span class="token punctuation">(</span>参数<span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="12-3-匿名函数"><a href="#12-3-匿名函数" class="headerlink" title="12.3 匿名函数"></a>12.3 匿名函数</h4><p>函数调用方式： 函数名()</p>
<p>匿名函数调用方法： ( function(){函数体} ) ()    //定义完立即调用</p>
<p>匿名函数调用方法： ( function(){函数体} () )   //定义完立即调用</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">
<span class="token keyword">var</span> 变量名 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token function">funtion</span> <span class="token punctuation">(</span><span class="token parameter">形参</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>                <span class="token comment">//定义完立即调用    </span>
    <span class="token keyword">return</span> 返回值<span class="token punctuation">;</span> 
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">(</span>参数<span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="12-4-this-语句"><a href="#12-4-this-语句" class="headerlink" title="12.4 this 语句"></a>12.4 this 语句</h4><blockquote>
<p>函数会传一个隐含的参数this，this指向一个对象。</p>
<p>这个对象被称为函数执行的上下文对象。</p>
</blockquote>
<p>根据函数的调用方式的不同，this会指向不同的对象。</p>
<ol>
<li>以函数的形式调用时，this永远都是window</li>
<li>以方法的形式调用时，this就是调用方法的那个对象。</li>
</ol>
<p>  this广义：</p>
<ul>
<li>在全局作用域或者普通函数中的this指向全局对象window。(==定时器中的this指向window==)</li>
<li>方法调用中谁调用this指向谁。</li>
<li>构造函数中this指向构造函数的实例。</li>
</ul>
<h3 id="13作用域"><a href="#13作用域" class="headerlink" title="13作用域"></a>13作用域</h3><blockquote>
<p>指定变量再指定范围内可以使用，为了提高程序的可靠性和减少命名冲突。</p>
<ul>
<li>全局作用域： 整个script标签或者一个单独的js文件。</li>
<li>局部作用域：在函数的内部。 </li>
</ul>
</blockquote>
<h4 id="局部变量和全局变量"><a href="#局部变量和全局变量" class="headerlink" title="局部变量和全局变量"></a>局部变量和全局变量</h4><p>全局变量： 在全局作用域下声明的变量。在函数内部没有声明直接赋值的变量也是全局变量。</p>
<p>​    * 全局变量在代码的任何位置都可以使用。</p>
<p>局部变量： 在局部作用域下的变量。函数的形参也可以看作局部变量。</p>
<p>​    * 局部变量只能在该函数内部使用。</p>
<p>执行效率：</p>
<ul>
<li>全局变量在任何地方都可以使用，只有在浏览器关闭时才会被销毁，因此比较占用内存。</li>
<li>局部变量只能在函数内部使用，当其所在的代码块被执行时，会被初始化，当代码运行结束后，会被销毁，因此更节省内存空间。</li>
</ul>
<h4 id="块级作用域"><a href="#块级作用域" class="headerlink" title="块级作用域"></a>块级作用域</h4><blockquote>
<p>js中es6才开始使用块级作用域。</p>
<p>块级作用域就是{}包含的作用域。</p>
</blockquote>
<h4 id="作用域链"><a href="#作用域链" class="headerlink" title="作用域链"></a>作用域链</h4><blockquote>
<p>根据内部函数可以访问外部函数变量这种机制，用链式查找哪些数据能被内部函数</p>
</blockquote>
<p>访问，就称作作用域链。</p>
<p>作用域链的访问规律：就近原则。</p>
<h3 id="13-5-变量提升"><a href="#13-5-变量提升" class="headerlink" title="13.5 变量提升"></a>13.5 变量提升</h3><blockquote>
<p>js引擎运行js分为两步：  变量提升   代码执行</p>
<p>==变量提升js引擎会把js里面所有的var和function提升到当前域最前面。==</p>
<p>代码执行按照diamagnetic书写的顺序从上往下执行。</p>
<p>浏览器预解析 包含变量提升</p>
</blockquote>
<h4 id="1-变量提升"><a href="#1-变量提升" class="headerlink" title="1.变量提升"></a>1.变量提升</h4><blockquote>
<p><strong>变量提升</strong>分为变量提升(变量提升) 和 函数提升(函数提升)</p>
</blockquote>
<blockquote>
<ol>
<li>变量提升就是把所有变量声明提升到当前作用域的最前面，不提升赋值操作。</li>
<li>函数提升就是把所有的函数声明提升到当前作用域的最前面，不调用函数。</li>
</ol>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> <span class="token function-variable function">fun</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//函数表达式调用必须写在函数表达式的下面，因为只提升变量声明，未提升函数声明。</span>
<span class="token keyword">var</span> a <span class="token operator">=</span> b <span class="token operator">=</span> c <span class="token operator">=</span> <span class="token number">9</span><span class="token punctuation">;</span>   <span class="token comment">//相当于： var a = 9; b = 9; c = 9;</span>
<span class="token keyword">var</span> a<span class="token operator">=</span><span class="token number">9</span><span class="token punctuation">,</span>b<span class="token operator">=</span><span class="token number">9</span><span class="token punctuation">,</span>c<span class="token operator">=</span><span class="token number">9</span><span class="token punctuation">;</span>   <span class="token comment">//相当于：  var a=9； var b=9 ; var c=9;</span>
					<span class="token comment">// var 表示定义局部变量，不带var表示定义全局变量</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

</blockquote>
<h3 id="13-垃圾回收"><a href="#13-垃圾回收" class="headerlink" title="13* 垃圾回收"></a>13* 垃圾回收</h3><blockquote>
<p>  在js中无法被再次使用的内存空间就是垃圾，无法主动处理垃圾，只能由浏览器自己处理垃圾，垃圾积攒过多会导致程序运行过慢，消耗内存空间。</p>
<p>  使用完变量后，如果想利用垃圾处理系统处理掉此变量，就给相应的变量或者对象名赋值为NULL 浏览器中的垃圾处理系统就会自动处理。</p>
</blockquote>
<h3 id="14-对象-內建"><a href="#14-对象-內建" class="headerlink" title="14 对象(內建)"></a>14 对象(內建)</h3><blockquote>
<p>內建对象就是浏览器自带的对象，不是用户定义的。提供了一些常用或者简单的功能。</p>
<p>对象是一个具体的实物。在js中对象是一组无序的相关属性和方法的集合，所有的事物都是对象，例如 字符串，数值，数组，函数。</p>
</blockquote>
<p>对象由属性和方法组成的：</p>
<p>属性：事物的特征，在对象中使用属性来表示。</p>
<p>方法： 事物的行为，在对象中使用方法来表示。</p>
<h4 id="14-1-数组"><a href="#14-1-数组" class="headerlink" title="14.1 数组"></a>14.1 数组</h4><blockquote>
<p>数组也是一个对象，与其他普通的对象功能类似，也是用来储存一些值的。</p>
<p>数组对象不同于普通对象使用字符串作为属性名，数组使用索引操作元素。</p>
<p>数组的存储性能比普通对象好，常使用数组来存储一些数据。</p>
</blockquote>
<pre class="line-numbers language-javas" data-language="javas"><code class="language-javas">var arr1 &#x3D; new Array();  &#x2F;&#x2F;创建了一个空数组
var arr2 &#x3D; new Array(2);  &#x2F;&#x2F;创建了一个长度为2的空数组
var arr3 &#x3D; new Array(2,3); &#x2F;&#x2F;等价于[2,3]创建了一个长度为2的数组，其中元素为 2和3。<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>







<h5 id="14-1-1数组的属性"><a href="#14-1-1数组的属性" class="headerlink" title="14.1.1数组的属性"></a>14.1.1数组的属性</h5><ul>
<li>数组定义语法：</li>
</ul>
<p>var 数组名 = new Array();</p>
<p>​    var 数组名 = [ , , ,]</p>
<ul>
<li>向数组中传入元素：</li>
</ul>
<p>数组名[下标]  =  值；</p>
<h6 id="1-length"><a href="#1-length" class="headerlink" title="1. length"></a>1. length</h6><ul>
<li>查询数组中有多少元素(<code>也可以设置数组中有多少元素</code>)：</li>
</ul>
<p>​     alert( 数组名.length);    //数组对象中length属性中存储这<code>顺序数组</code>中的元素个数</p>
<p>​    数组名.length = 数值     //设置数组中长度为多少，如果超出，超出的元素将会被删除。</p>
<ul>
<li>向顺序数组最后一个元素赋值</li>
</ul>
<p>数组名[数组名.length] = 值； //length是数组最后一个元素的下标加1</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//定义数组对象</span>
								<span class="token comment">//创建一个10个元素的数组</span>
<span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token operator">||</span>  <span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
								<span class="token comment">//创建一个数组,其中有一个元素是10</span>
<span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>				<span class="token operator">||</span> <span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">//向数组中传入元素：</span>
arrayName<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
arrayName<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
arrayName<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token comment">//可以不按顺序赋值，中间的值会被留空，如果输出未赋值的数组元素，不会报错，会输出undfined</span>
arrayName<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

<span class="token comment">//向顺序数组最后一个元素赋值</span>

arrayName<span class="token punctuation">[</span>arrayName<span class="token punctuation">.</span>length<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h5 id="14-1-2-数组的方法"><a href="#14-1-2-数组的方法" class="headerlink" title="14.1.2 数组的方法"></a>14.1.2 数组的方法</h5><h6 id="instanceof-amp-amp-isArray"><a href="#instanceof-amp-amp-isArray" class="headerlink" title="instanceof &amp;&amp; isArray()"></a>instanceof &amp;&amp; isArray()</h6><blockquote>
<p>检测变量是为数组</p>
</blockquote>
<pre class="line-numbers language-javas" data-language="javas"><code class="language-javas">var arr &#x3D; [];
console.log(arr instanceof Array);
Array.isArray(arr);<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<table>
<thead>
<tr>
<th>方法名</th>
<th>说明</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>push(参数，参数)</td>
<td>末尾添加一个或者多个元素</td>
<td>返回新的长度</td>
</tr>
<tr>
<td>pop()</td>
<td>删除数组最后一个元素，把数组长度减一，无参数，==修改原数组==</td>
<td>返回新的长度</td>
</tr>
<tr>
<td>unshift()</td>
<td>像数组的开头添加一个或者更多元素，==修改原数组==</td>
<td>返回新的长度</td>
</tr>
<tr>
<td>shift()</td>
<td>删除数组的第一个元素，数组长度减一，无参数，==修改原数组==</td>
<td>返回第一个元素的值</td>
</tr>
</tbody></table>
<h6 id="push"><a href="#push" class="headerlink" title="push()"></a>push()</h6><blockquote>
<p>该方法可以向数组的末尾添加一个或者多个元素，并返回数组新的长度。</p>
<p>可以将要插入的元素作为方法的参数传递，这些元素将会自动的添加到数组的末尾。</p>
<p>该方法会将数组新的长度作为返回值返回。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//push()将会将参数添加到数组的末尾，并且返回数组中有多少个值。</span>
<span class="token keyword">var</span> len <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h6 id="pop"><a href="#pop" class="headerlink" title="pop()"></a>pop()</h6><blockquote>
<p>该方法会删除数组最后一个元素，并且将最后一个元素作为返回值返回。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//pop()方法会返回数组最后一个元素的值并删除最后一个值。</span>
<span class="token keyword">var</span> i <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h6 id="unshift"><a href="#unshift" class="headerlink" title="unshift()"></a>unshift()</h6><blockquote>
<p>unshift()方法用于在数组的前方插入一个或者多个值，值用参数接受并传递。</p>
<p>并且插入之后还会返回数组元素的个数。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//unshift()在数组的前方添加值，返回数组元素的个数。</span>
<span class="token keyword">var</span> i <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h6 id="shift"><a href="#shift" class="headerlink" title="shift()"></a>shift()</h6><blockquote>
<p>shift()方法用于在数组前方删除第一个元素，并将被删除的元素作为返回值返回。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//shift()在数组的前方删除值，返回数组元素的个数。</span>
<span class="token keyword">var</span> i <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h6 id="forEach"><a href="#forEach" class="headerlink" title="forEach()"></a>forEach()</h6><blockquote>
<p>forEach()需要传入一个回调函数作为参数，在forEach()中，数组有几个元素就会调用回调函数几次，每次执行，浏览器会将遍历到的元素以实参的形式传入到回调函数，可以在回调函数中定义形参来接受函数传入的值。</p>
</blockquote>
<p>forEach()会传入三个值：</p>
<ul>
<li>当前元素的值</li>
<li>当前元素的索引</li>
<li>当前数组</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span>index<span class="token punctuation">,</span>array</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
           console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//输出传入元素的值</span>
    		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//输出传入元素的下标</span>
   	    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//输出传入数组</span>
       <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h6 id="indexOf"><a href="#indexOf" class="headerlink" title="indexOf()"></a>indexOf()</h6><blockquote>
<p>返回数组只当元素的索引号</p>
<p>只返回第一个满足条件的索引号</p>
<p>如果没有找到返回-1</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arrayName<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h6 id="lastIndexOf"><a href="#lastIndexOf" class="headerlink" title="lastIndexOf()"></a>lastIndexOf()</h6><blockquote>
<p>返回数组只当元素的索引号</p>
<p>只返回最后一个满足条件的索引号</p>
<p>如果没有找到返回-1</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arrayName<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h6 id="slice"><a href="#slice" class="headerlink" title="slice()"></a>slice()</h6><blockquote>
<p>slice()可以用来从数组中提取元素并返回。</p>
<p>==该方法不会影响原数组==，而是将截取到的元素封装到一个新的数组中并返回。</p>
</blockquote>
<p>可以传递两个参数：</p>
<ul>
<li><p>第一个参数：截取的开始位置，包含开始索引。</p>
</li>
<li><p>第二个参数：截取的结束位置，不包含结束索引。—-可以省略不写，会截取开始索引以后的所有元素。</p>
</li>
</ul>
<p>可以传递负值，传递负值会从后往前计算。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//从第一个元素开始，到第二个元素结束。包含1不包含2，不会改变原数组。</span>
<span class="token keyword">var</span> arrayName2 <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h6 id="splice"><a href="#splice" class="headerlink" title="splice()"></a>splice()</h6><blockquote>
<p>会弹出数组中的指定值。</p>
<p>==会影响到原数组==，会将指定的元素从原数组中删除。并将删除的元素作为返回值。</p>
</blockquote>
<p>可以传递多个参数：</p>
<ul>
<li>第一个参数：表示开始的位置的索引。</li>
<li>第二个参数：表示删除的数量。</li>
<li>其他参数：传递一些新的元素，这些元素将会自动的插入到开始索引的前边。</li>
</ul>
<p>如果第二个参数是零，就是单纯的在第一个参数的索引前添加相应的元素。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arrayName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"这是第一个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第二个元素"</span><span class="token punctuation">,</span><span class="token string">"这是第三个元素"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">//从第一个元素开始，删除两个元素，然后再将所指定的元素添加到索引为1的前边。</span>
<span class="token keyword">var</span> arrayName2 <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">"添加的元素"</span><span class="token punctuation">,</span><span class="token string">"添加的元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
<span class="token comment">//单纯的再索引为1前添加指定的元素。</span>
<span class="token keyword">var</span> arrayName2 <span class="token operator">=</span> arrayName<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">"添加的元素"</span><span class="token punctuation">,</span><span class="token string">"添加的元素"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h6 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h6><blockquote>
<p>用于链接两个或者多个数组，并返回新的数组。</p>
<p>该方法不会对原来的数组产生影响。</p>
<p>语法：</p>
<ul>
<li>var 新数组名 = 数组1.concat(数组2)</li>
<li>var 新数组名 = 数组1.concat(数组2，数组3)</li>
<li>var 新数组名 = 数组1.concat(数组2，数组3，”元素”)</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//将连接的数组返回，不会改变原来的各个数组 。</span>
<span class="token keyword">var</span> NewArrayName <span class="token operator">=</span> ArrayName<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>ArrayName1<span class="token punctuation">,</span>ArrayName2<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h6 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString()"></a>toString()</h6><blockquote>
<p>toString()把数组换成字符串，逗号分隔每一项</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'d'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h6 id="join"><a href="#join" class="headerlink" title="join()"></a>join()</h6><blockquote>
<p>该方法可以将一个数组转换为一个字符串</p>
<p>该方法不会对数组产生影响。而是将转换后的字符串作为结果返回。</p>
<p>可以在join()中指定一个字符串作为参数，这个字符串会成为数组元素连接的中间字符。</p>
<p>如果不指定连接的字符，将会默认使用逗号作为连接符。</p>
</blockquote>
<p>语法：</p>
<ul>
<li>var 新数组名 = 数组名.join(“字符”)；</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> NewArrayName <span class="token operator">=</span> ArrayName<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">"&amp;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="reverse"><a href="#reverse" class="headerlink" title="reverse()"></a>reverse()</h6><blockquote>
<p>给方法用来反转数组。</p>
<p>该方法会直接==修改原数组。==</p>
</blockquote>
<p>语法：</p>
<ul>
<li>数组名.reverse();</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">ArrayName<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="sort"><a href="#sort" class="headerlink" title="sort()"></a>sort()</h6><blockquote>
<p>可以用来对数组的元素进行排序。</p>
<p>也会影响原数组，默认使用的是Unicode编码排序。</p>
</blockquote>
<p>语法：</p>
<ul>
<li>数组名.sort();</li>
</ul>
<p>即使对于纯数字的数组，使用sort()排序时，也会按照Unicode编码来排序，有时对数字进行排序时，会得到错误的结果。</p>
<p>可以在sort中指定回调函数，传入两个实参，根据函数的返回值判断是否会交换顺序，返回正数时会交换顺序，返回0时默认相等，不会交换顺序，返回负值时不会交换顺序。</p>
<p>语法：</p>
<ul>
<li>数组名.sort(function (a,b){</li>
</ul>
<p>​    return: a-b;</p>
<p>});</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">ArrayName<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

ArrayName<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span>b</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">return</span> a<span class="token operator">-</span>b<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="14-2-函数"><a href="#14-2-函数" class="headerlink" title="14.2 函数"></a>14.2 函数</h4><blockquote>
<p>函数也是对象，也拥有属性和方法。</p>
<p>函数就是封装了一段可以被重复使用的代码块，可以通过此代码块实现大量代码的重复使用。</p>
<p>函数需要在定义之后调用才会执行。</p>
</blockquote>
<h5 id="函数声明方式"><a href="#函数声明方式" class="headerlink" title="函数声明方式"></a>函数声明方式</h5><p>利用函数关键字自定义函数(命名函数).</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>函数表达式。</p>
<ul>
<li>函数表达式声明方式和声明变量差不多，只不过变量里存储的是值，而    函数表达式中存储的是函数。</li>
<li>可以通过 变量名() 方式调用函数。</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> <span class="token function-variable function">变量名</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>





<h5 id="形参和实参"><a href="#形参和实参" class="headerlink" title="形参和实参"></a>形参和实参</h5><blockquote>
<p>声明函数时，可以在函数名称后的小括号中添加一些参数，这些参数被称为形参，而在调用该参数时，同样也需要传递相应的参数，这些参数被称为实参。</p>
<ul>
<li>形参：形式上的参数，函数定义的时候传递的参数，当前并不知道具体的值。</li>
<li>实参：实际上的参数，函数调用用的时候传递的参数，实参时传递给形参的。</li>
</ul>
</blockquote>
<p>实参和形参不匹配：</p>
<ul>
<li>如果实参的个数和形参的个数一致，则正常输出结果。</li>
<li>如果实参的个数多于形参的个数，会取到形参的个数。</li>
<li>如果实参的个数小于形参的个数，未被传递实参的形参会附上默认值: undefined。</li>
</ul>
<h5 id="函数的返回值："><a href="#函数的返回值：" class="headerlink" title="函数的返回值："></a>函数的返回值：</h5><blockquote>
<p>函数的返回值使用return语句返回指定数据。</p>
</blockquote>
<p>只要函数遇到return就会将后面的结果返回到函数的调用者。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
<span class="token keyword">return</span> 返回值；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ul>
<li>==return只能返回一个值，返回的是最后一个值==。</li>
<li>需要使用return返回多个值时可以封装成数组。</li>
<li>所有函数都有返回值，如果未指定return则返回undefined。</li>
</ul>
<h5 id="函数的属性"><a href="#函数的属性" class="headerlink" title="函数的属性"></a>函数的属性</h5><h6 id="arguments"><a href="#arguments" class="headerlink" title="arguments"></a>arguments</h6><blockquote>
<p>arguments存储这传递过来的实参。</p>
<p>arguments是一个伪数组。伪数组可以使用length和按照索引的方式进行存储但是不能使用真正数组中的一些方法。</p>
<p>只有函数才有arguments对象。</p>
</blockquote>
<p>在调用函数时，浏览器每次都会传递==两个隐含的参数，==</p>
<ol>
<li><p>函数的上下文对象 this</p>
</li>
<li><p>封装实参的对象 arguments</p>
</li>
</ol>
<p>​    arguments是一个类数组对象，他也可以通过索引来操作数据，也可以获取长度。</p>
<ul>
<li><p>在调用函数时，传递的实参都会在arguments中保存。arguments是一个类数组对象。</p>
</li>
<li><p>即使不定义形参，也可以通过arguments来使用实参，只不过比较麻烦。</p>
<ul>
<li>arguments[0]  表示第一个实参。</li>
<li>arguments[1]  表示第二个实参。</li>
</ul>
</li>
<li><p>arguments.callee 属性：</p>
</li>
<li><p>是当前正在运行的函数对象。s</p>
</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>arguments<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>

<span class="token function">fun</span><span class="token punctuation">(</span><span class="token string">"第一个形参"</span><span class="token punctuation">,</span><span class="token string">"第二个形参"</span><span class="token punctuation">)</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
<span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>arguments<span class="token punctuation">.</span>callee<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//这条语句会输出当前这个函数所有的语句,包括注释</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="函数的方法"><a href="#函数的方法" class="headerlink" title="函数的方法"></a>函数的方法</h5><h6 id="call-和-apply"><a href="#call-和-apply" class="headerlink" title="call() 和 apply()"></a>call() 和 apply()</h6><blockquote>
<p>这两个函数都是函数对象的方法，需要通过函数的对象来调用。</p>
<p>当对函数调用call()和apply()都会调用函数执行。</p>
<p>在调用call()和apply()可以将一个对象指定为第一个参数。</p>
<p>​    <code>此时这个对象将会成为这个函数执行时的this。</code></p>
</blockquote>
<p>call()方法可以将实参在对象之后依次传递，</p>
<p>apply()方法需要将实参封装到数组中在对象之后统一传递。</p>
<p>语法：</p>
<p>function 函数名 ( ){</p>
<p>​    函数执行语句；        </p>
<p>}</p>
<p>函数名.call(对象名);</p>
<p>函数名.apply(对象名);</p>
<h4 id="回调函数"><a href="#回调函数" class="headerlink" title="回调函数"></a>回调函数</h4><blockquote>
<p>可以在定义函数时定义一个函数形参，当调用函数时传递函数到实参，在函数最后调用函数。这时候这个函数被当作一个普通的实参处理。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token parameter">callback</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">function</span> <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"这是一个回调函数"</span><span class="token punctuation">)</span>；
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"这是一个回调函数"</span><span class="token punctuation">)</span>；
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
  <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="节流阀"><a href="#节流阀" class="headerlink" title="节流阀"></a>节流阀</h4><blockquote>
<p>节流阀可以强制使一个函数执行完毕再去执行下一次函数。</p>
<p>利用回调函数，添加一个变量来控制，锁住函数和解锁函数。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token parameter">callback</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>      <span class="token comment">//判断是否能执行</span>
 	falg <span class="token operator">=</span> <span class="token boolean">false</span>；
  	<span class="token comment">//函数执行代码</span>
  <span class="token punctuation">&#125;</span>
<span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">//回调函数</span>
<span class="token keyword">function</span> <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<hr>
<h4 id="14-3-Date"><a href="#14-3-Date" class="headerlink" title="14.3 Date"></a>14.3 Date</h4><blockquote>
<p>Data对象封装着一个时间对象，需要在构造函数中传递一个表示时间的字符串作为参数。</p>
<p>Data只能通过构造函数来实例化日期对象。</p>
<p>日期的格式 月份/日/年 时:分:秒</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//直接调用封装着代码运行时的时间。</span>
<span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//传入参数调用时封装的传入参数指定的时间。</span>
<span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">"12/03/2011 11:10:30"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>如果没有跟参数返回当前系统的当前时间。</li>
<li>参数常用： 数字型 2018，1，1  字符串”1018-1-1 8:8:9“</li>
<li>数字型的月份会比实际月份大一。</li>
</ul>
<h5 id="Data的方法"><a href="#Data的方法" class="headerlink" title="Data的方法"></a>Data的方法</h5><p>获取当前时间</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//获取当前时间以时分秒形式输出</span>
<span class="token keyword">function</span> <span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         <span class="token keyword">var</span> time <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">;</span>
         <span class="token keyword">var</span> h <span class="token operator">=</span> time<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         h <span class="token operator">=</span> h <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> h <span class="token operator">:</span> h<span class="token punctuation">;</span>
         <span class="token keyword">var</span> m <span class="token operator">=</span> time<span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			m <span class="token operator">=</span> m <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> m <span class="token operator">:</span> m<span class="token punctuation">;</span>
         <span class="token keyword">var</span> s <span class="token operator">=</span> time<span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			s <span class="token operator">=</span> s <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> s <span class="token operator">:</span> s<span class="token punctuation">;</span>
         <span class="token keyword">return</span> h <span class="token operator">+</span> <span class="token string">":"</span> <span class="token operator">+</span> m <span class="token operator">+</span> <span class="token string">":"</span> <span class="token operator">+</span> s <span class="token punctuation">;</span>
     <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//获得当前时间以年月日星期格式返回</span>
<span class="token keyword">function</span> <span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         <span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">;</span>
         <span class="token keyword">var</span> year <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">var</span> month <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
         <span class="token keyword">var</span> dates <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"星期日"</span><span class="token punctuation">,</span><span class="token string">"星期一"</span><span class="token punctuation">,</span><span class="token string">"星期二"</span><span class="token punctuation">,</span><span class="token string">"星期三"</span><span class="token punctuation">,</span><span class="token string">"星期四"</span><span class="token punctuation">,</span><span class="token string">"星期五"</span><span class="token punctuation">,</span><span class="token string">"星期六"</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
         <span class="token keyword">var</span> day <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getDay</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">return</span> year <span class="token operator">+</span> <span class="token string">"年"</span> <span class="token operator">+</span> month <span class="token operator">+</span> <span class="token string">"月"</span> <span class="token operator">+</span> dates <span class="token operator">+</span> <span class="token string">"日"</span> <span class="token operator">+</span> arr<span class="token punctuation">[</span>day<span class="token punctuation">]</span> <span class="token punctuation">;</span>
     <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h6 id="getDate"><a href="#getDate" class="headerlink" title="getDate()"></a>getDate()</h6><blockquote>
<p>获取当前日期对象的日期。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">"12/03/2011 11:10:30"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h6 id="getDay"><a href="#getDay" class="headerlink" title="getDay()"></a>getDay()</h6><blockquote>
<p>获取当前日期是周几。</p>
<p>返回一个0~6的数。周日返回的是0。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">x <span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">"12/03/2011 11:10:30"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span><span class="token function">getDay</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h6 id="getMonth"><a href="#getMonth" class="headerlink" title="getMonth()"></a>getMonth()</h6><blockquote>
<p>获取当前时间对象的月份。</p>
<p>返回0~11的值(<code>与中国月份并不等值</code>)</p>
<p>​    0 代表 1月，11 代表 12月</p>
</blockquote>
<h6 id="getFullYear"><a href="#getFullYear" class="headerlink" title="getFullYear()"></a>getFullYear()</h6><blockquote>
<p>获取当前对象的年份，以四位数字返回年份</p>
</blockquote>
<h6 id="getYear"><a href="#getYear" class="headerlink" title="getYear()"></a>getYear()</h6><blockquote>
<p>获取当前对象的年份，以两位数字返回年份。</p>
<p>不推荐使用，用getFullYear()方法替代。</p>
</blockquote>
<h6 id="getTime-valueOf"><a href="#getTime-valueOf" class="headerlink" title="getTime(),valueOf()"></a>getTime(),valueOf()</h6><blockquote>
<p>获取当前日期对象的时间戳。</p>
<p>时间戳：指从格林威治标准时间的1970年1月1日，0时0分0秒到当前日期所花费的毫秒数。</p>
<p>标准时间的换算单位非常繁杂，使用时间戳可以减少换算量。</p>
<p>计算机底层保存时间都使用的是时间戳。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> time <span class="token operator">=</span> <span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
date<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
date<span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>倒计时</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//接收时间，得到倒计时时间</span>
     <span class="token keyword">function</span> <span class="token function">countDown</span><span class="token punctuation">(</span><span class="token parameter">time</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         <span class="token keyword">var</span> nowTime <span class="token operator">=</span> <span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获得当前时间</span>
         <span class="token keyword">var</span> inputTime <span class="token operator">=</span> <span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获得输入时间</span>
         <span class="token keyword">var</span> times  <span class="token operator">=</span> <span class="token punctuation">(</span>inputTime <span class="token operator">-</span> nowTime<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1000</span><span class="token punctuation">;</span><span class="token comment">//获得相差秒数</span>
         <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>times<span class="token operator">/</span><span class="token number">60</span><span class="token operator">/</span><span class="token number">60</span><span class="token operator">/</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      	d <span class="token operator">=</span> d <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> d <span class="token operator">:</span> d<span class="token punctuation">;</span>
         <span class="token keyword">var</span> h <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>times<span class="token operator">/</span><span class="token number">60</span><span class="token operator">/</span><span class="token number">60</span><span class="token operator">%</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       	h <span class="token operator">=</span> h <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> h <span class="token operator">:</span> h<span class="token punctuation">;</span>
         <span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>times<span class="token operator">/</span><span class="token number">60</span><span class="token operator">%</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       	m <span class="token operator">=</span> m <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> m <span class="token operator">:</span> m<span class="token punctuation">;</span>
         <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>times<span class="token operator">%</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       	s <span class="token operator">=</span> s <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token operator">?</span><span class="token string">"0"</span> <span class="token operator">+</span> s <span class="token operator">:</span> s<span class="token punctuation">;</span>
         <span class="token keyword">return</span> d <span class="token operator">+</span> <span class="token string">"天"</span> <span class="token operator">+</span> h <span class="token operator">+</span> <span class="token string">"时"</span> <span class="token operator">+</span> m <span class="token operator">+</span> <span class="token string">"分"</span> <span class="token operator">+</span> s <span class="token operator">+</span> <span class="token string">"秒"</span><span class="token punctuation">;</span> 
     <span class="token punctuation">&#125;</span>
     <span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token string">"2022-2-26 12:00:00"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="14-4-Math"><a href="#14-4-Math" class="headerlink" title="14.4 Math"></a>14.4 Math</h4><blockquote>
<p>Math和其他的对象不同，它不是一个构造函数，它属于一个工具类不用创建，它里面封装了数学运算相关的属性和方法。</p>
<ul>
<li>Math并不是构造器，所有的方法和属性都是静态的。</li>
</ul>
</blockquote>
<p>使用举例：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token function">alert</span><span class="token punctuation">(</span> Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">//输出输入数字的绝对值</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="Math-random"><a href="#Math-random" class="headerlink" title="Math.random()"></a>Math.random()</h6><blockquote>
<p>可以生成一个0-1之间的随机数。</p>
<p>生成一个 x-y之间的随机整数：</p>
<ul>
<li>Math.round(Math.random() * (y-x) +x);</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//返回两个数之间的整数包含两个数</span>
<span class="token keyword">function</span> <span class="token function">getRandom</span><span class="token punctuation">(</span><span class="token parameter">min<span class="token punctuation">,</span>max</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
 <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span>random<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>max <span class="token operator">-</span> min <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> min<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<p>取整函数</p>
<blockquote>
<ul>
<li>Math.floor() 向下取整</li>
<li>Math.ceil() 向上取整</li>
<li>Math.round() 四舍五入（.5往大了取）</li>
</ul>
</blockquote>
<h4 id="14-5-包装类"><a href="#14-5-包装类" class="headerlink" title="14.5 包装类"></a>14.5 包装类</h4><p>基本数据类型</p>
<p>​    String Number Boolean Null Undefined</p>
<p>引用数据类型</p>
<p>​    Object</p>
<p>在JS中为提供了三个包装类，通过三个包装类可以将基本数据类型的数据转换为对象：</p>
<p>​    String():可以将基本数据类型字符串转换为String对象。</p>
<p>​    Number():可以将基本数据类型的数字转换为Number对象。</p>
<p>​    Boolean():可以将基本数据类型的布尔值转换为Boolean对象。</p>
<p>注意：实际应用中不会使用基本数据类型的对象，如果使用基本数据类型的对象，在做一些比较时可能会带来一些不可预计的结果。</p>
<p>方法和属性能添加给对象，不能添加给基本数据类型。</p>
<p>当对基本数据类型的值调用属性或者方法时：（如： str.toString() )</p>
<p>浏览器会临时使用包装类将其转换为对象，然后再调用对象的属性和方法。</p>
<p>调用完之后，会再将其转换为基本类型。</p>
<h5 id="string类的属性"><a href="#string类的属性" class="headerlink" title="string类的属性"></a>string类的属性</h5><blockquote>
<p>底层字符串是以字符数组的形式储存的。可以使用 字符串名[下标] 的方式访问指定字符。</p>
<p>length: 调用 类名.length 可以返回当前字符串的长度。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span>lengh<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//基本包装类型：就是把简单数据类型包装成为了复杂数据类型。</span>
<span class="token number">1.</span>把简单数据类型把包装为复杂数据类型。基本数据类型就有了属性和方法。
<span class="token keyword">var</span> temp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'andy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token number">2.</span>把临时变量的值给str
str <span class="token operator">=</span> temp；
<span class="token number">3.</span>销毁这个临时变量
temp <span class="token operator">=</span> <span class="token keyword">null</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>字符串的不变性:</p>
<ul>
<li>字符串不可变,指的是里面的值不可变,虽然看上去可以改变内容,但是其实是地址变了,内存中开辟了一个新的内存空间.</li>
</ul>
<h5 id="string类的方法："><a href="#string类的方法：" class="headerlink" title="string类的方法："></a>string类的方法：</h5><blockquote>
<p>字符串所有的方法,==都不会修改字符串本身==(字符串是不可变的),操作完成后会返回一个新的字符串.</p>
</blockquote>
<h6 id="charAt"><a href="#charAt" class="headerlink" title="charAt()"></a>charAt()</h6><blockquote>
<p>获取指定位置的字符，相当于 字符串名[下标]</p>
<p>不会改变原来的字符串。</p>
<p>语法：</p>
<p>​    var 新字符串名 = 字符串名.charAt(下标);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> strName <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//相当于：</span>
<span class="token keyword">var</span> strName <span class="token operator">=</span> str<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>  <span class="token comment">//h5新增,在java中没有这种用法</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//获取字符串中字符的个数</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h6 id="charCodeAt"><a href="#charCodeAt" class="headerlink" title="charCodeAt()"></a>charCodeAt()</h6><blockquote>
<p>获取指定字符串指定下标的字符编码(Unicode编码)</p>
<p>多用于判断用户按下哪个键.</p>
</blockquote>
<p>不会改变原字符串。</p>
<p>语法： </p>
<p>​    var 新字符串名 = 字符串名.charCodeAt(下标);</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> strName <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="fromCharCode"><a href="#fromCharCode" class="headerlink" title="fromCharCode()"></a>fromCharCode()</h6><blockquote>
<p>这个函数需要通过构造对象去调用</p>
<p>接受一个Unicode编码，返回对应的字符。</p>
<p>语法： </p>
<p>​        var 字符串名 = String.fromCharCode(编码);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> strName <span class="token operator">=</span> String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="concat-1"><a href="#concat-1" class="headerlink" title="concat()"></a>concat()</h6><blockquote>
<p>可以链接两个或多个字符串</p>
<p>作用和 + 号一样,+号方式更加常用</p>
<p>不会改变原来的字符串</p>
<p>语法：</p>
<p>​    var 新字符串名 = 字符串名.concat(“ “ ,” “);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> strName <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">" "</span> <span class="token punctuation">,</span><span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="indexOf-amp-lastIndexOf"><a href="#indexOf-amp-lastIndexOf" class="headerlink" title="indexOf()  &amp;  lastIndexOf()"></a>indexOf()  &amp;  lastIndexOf()</h6><p>indexOf():</p>
<p>​    该方法用于检测一个字符串中是否含有指定的内容</p>
<p>​    如果字符串含有指定的内容，就会返回第一次出现的索引</p>
<p>​    如果没有找到指定的内容，则会返回-1</p>
<p>​    可以指定第二个参数，从指定位置开始查找</p>
<p>lastIndexOf():</p>
<p>​    该方法的用法和indexOf()一样，不同的是从后往前找。</p>
<p>语法： </p>
<p>​    var result = 字符串名.indexOf(“str”,下标)；</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"str"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//在变量str中查找'str'字符串,从最开始开始查找</span>
<span class="token keyword">var</span> result1 <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"str"</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//在变量str中查找'str'字符串,从下标为2开始查找</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h6 id="slice-1"><a href="#slice-1" class="headerlink" title="slice()"></a>slice()</h6><p>可以从字符串中截取指定的内容</p>
<p>不会影响原字符串，而是将截取的内容返回。</p>
<p>参数： </p>
<p>​    第一个：开始位置的索引(包括开始位置)</p>
<p>​    第二个：结束位置的索引(不包括结束位置)</p>
<p>​            如果省略第二个参数，就会截取到以后所有的字符</p>
<p>​            可以传递负数，会从后往前计算。</p>
<p>语法：</p>
<p>​    var result = 字符串名.slice(开始位置，结束位置);</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="substring"><a href="#substring" class="headerlink" title="substring()"></a>substring()</h6><p>可以用来截取一个字符串，和slice()类似</p>
<p>参数： </p>
<p>​    第一个：开始位置的索引(包括开始位置)</p>
<p>​    第二个：结束位置的索引(不包括结束位置)</p>
<p>不同之处：</p>
<p>​    这个方法不能接受负值，如果传递了负值，会默认使用0</p>
<p>​    ==而且会自动调整参数的位置==，如果第二个参数小于第一个，会自动交换位置</p>
<h6 id="substr"><a href="#substr" class="headerlink" title="substr()"></a>substr()</h6><blockquote>
<p>用来截取字符串</p>
<p>参数：</p>
<ol>
<li>截取开始位置的索引</li>
<li>截取的长度</li>
</ol>
<p>语法： </p>
<p>​    var result = 字符串.substr(开始位置，长度);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="split"><a href="#split" class="headerlink" title="split()"></a>split()</h6><p>可以将一个字符串拆分为一个数组(join将数组转换为字符串)</p>
<p>不会影响到原来的字符串</p>
<p>参数：</p>
<p>​    需要一个字符串为参数，将会根据该字符串去拆分数组<code>(可以传递正则表达式)</code></p>
<p>​    如果传递空串会将字符串每个字符都拆分,使用正则表达式也会全部拆分</p>
<p>语法：</p>
<p>​    var result = 字符串.split(“分隔符”);</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">solit</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="toUpperCase-amp-toLowerCase"><a href="#toUpperCase-amp-toLowerCase" class="headerlink" title="toUpperCase()  &amp;  toLowerCase()"></a>toUpperCase()  &amp;  toLowerCase()</h6><p>将一个字符串转换为全大写或者全小写</p>
<p>不会影响到原来的字符串</p>
<p>语法： </p>
<p>​    var result = 字符串.toUppercase();</p>
<p>​    var result = 字符串.toLowerCase();</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">toUppercase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h6 id="search"><a href="#search" class="headerlink" title="search()"></a>search()</h6><p>可以搜索字符串中是否含有指定的内容</p>
<p>如果搜索到指定的内容,则会返回第一次出现的索引,如果没有搜索到,会返回-1</p>
<p>可以接受正则表达式作为参数,然后会根据正则表达式去检索字符串</p>
<p>语法: </p>
<p>​    var result = 字符串.search(‘str’);</p>
<p>​    var result = 字符串.search(/str/);</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string">'str'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">str</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h6 id="match"><a href="#match" class="headerlink" title="match()"></a>match()</h6><p>可以根据正则表达式,从一个字符串中将符合条件内容提取出来</p>
<p>默认情况下match只会找到第一个符合要求的内容,找到以后就会停止检索</p>
<p>​    可以设置正则表达式为全局匹配模式,这样就会匹配到所有的内容</p>
<p>​    可以为一个正则表达式设置多个匹配模式,且顺序无所谓</p>
<p>match()语句会将匹配到的内容封装到一个数组中返回,即使之查询到一个结果</p>
<p>语法:</p>
<p>​    var result = 字符串.match(/[a-z]/ig);</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[a-z]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">ig</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="replace"><a href="#replace" class="headerlink" title="replace()"></a>replace()</h6><p>可以将字符串中指定内容替换为新的内容</p>
<p>不会影响到原来的字符串</p>
<p>参数:</p>
<p>​    1.被替换的内容,可以接受一个正则表达式为参数</p>
<p>​    2.新的内容</p>
<p>==默认只会替换第一个==,删除指定的内容可以使用空串替换</p>
<p>语法:</p>
<p>​    var result = str.replace(“被替换的内容”,”新的内容”);</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> result <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"@"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>





<h4 id="14-6-正则类"><a href="#14-6-正则类" class="headerlink" title="14.6 正则类"></a>14.6 正则类</h4><p>创建正则表达式对象</p>
<p>语法：</p>
<p>​    var 类名 = new RegExp(“正则表达式”,”匹配模式”)</p>
<p>​    var 类名 = /正则表达式/匹配模式</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">,</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">/*输入字符串*/</span>
<span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h3 id="15-正则表达式"><a href="#15-正则表达式" class="headerlink" title="15 正则表达式"></a>15 正则表达式</h3><blockquote>
<p>正则表达式用于定义一些字符串的规则。</p>
<p>​    可以使计算机根据正则表达式，来检查一个字符串是否符合规则</p>
<p>​    获取字符串中符合规则的内容提取出来</p>
</blockquote>
<h4 id="15-1-创建正则表达式"><a href="#15-1-创建正则表达式" class="headerlink" title="15.1 创建正则表达式"></a>15.1 创建正则表达式</h4><blockquote>
<p>语法：</p>
<p>​    var 类名 = new RegExp(“正则表达式”,”匹配模式”)    (可以传递变量)</p>
<p>​    var 正则名 = /正则表达式/匹配模式      (使用字面量创建，不能传递变量)</p>
<p>匹配模式(可以填写多个匹配模式,顺序无所谓)：</p>
<p>​        i 忽略大小写   </p>
<p>​        g 全局匹配模式,会一直匹配下去</p>
<p>​        m 执行多行匹配</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"i"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//参数为字符串形式</span>
<span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h4 id="15-2-正则表达式的方法"><a href="#15-2-正则表达式的方法" class="headerlink" title="15.2 正则表达式的方法"></a>15.2 正则表达式的方法</h4><h5 id="test"><a href="#test" class="headerlink" title="test()"></a>test()</h5><blockquote>
<p>使用这个方法可以用来检测一个字符串是否符合正则表达式的规则</p>
<p>​    如果符合则返回true，如果不符合则返回falst</p>
<p>严格区分大小写(可以添加匹配模式 i 改为不严格区分大小写)</p>
<p>语法：</p>
<p>​    var reg = new RegExp(“正则表达式”,”匹配模式”)；</p>
<p>​    reg.test(字符串);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">"abc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h4 id="15-3-正则匹配规则"><a href="#15-3-正则匹配规则" class="headerlink" title="15.3 正则匹配规则"></a>15.3 正则匹配规则</h4><h5 id="正则-集合"><a href="#正则-集合" class="headerlink" title="正则:  集合"></a>正则:  集合</h5><blockquote>
<p>直接使用字符来匹配是否存在对应的字符,查找给定集合中的字符串</p>
<p>语法:</p>
<p>​    var reg = /字符串或字符/</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
str <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">//匹配是否str中是否存在a字符    </span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="正则-或"><a href="#正则-或" class="headerlink" title="正则:  或   |   []"></a>正则:  或   |   []</h5><blockquote>
<p>只能表示一个字符</p>
</blockquote>
<blockquote>
<p>在正则表达式中可以使用 | 来表示或</p>
<p>在正则表达式中也可以使用 [] 来表示或</p>
<p>语法:</p>
<p>​    var reg = /字符串1 | 字符串2/ ；</p>
<p>​    var reg = /[字符串1字符串2]/ ；</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a|b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[ab]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
str <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">//匹配是否str中是否存在a字符或者b字符</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="正则-省略"><a href="#正则-省略" class="headerlink" title="正则:  省略 -"></a>正则:  省略 -</h5><blockquote>
<p>在正则表达式中可以使用 - 来省略中间的字符串(只有几种)</p>
<p>只能表示一个字符</p>
<p>语法:</p>
<p>​    [a-z] : 任意小写字母</p>
<p>​    [A-Z] : 任意大写字母</p>
<p>​    [A-z] : 任意字母</p>
<p>​    [0-9] : 任意0到9数字</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[a-d]</span><span class="token regex-delimiter">/</span></span>
str <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//匹配字符串中是否含有a到d的字符</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="正则-非"><a href="#正则-非" class="headerlink" title="正则:  [^]  非"></a>正则:  [^]  非</h5><blockquote>
<p>在正则表达式中可以使用[^]来表示除了指定的字符串</p>
<p>只有在[]才表示非的意思,在[]外指代开头的意思</p>
<p>语法:</p>
<p>​    var reg = /[^字符串]/</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[^a]</span><span class="token regex-delimiter">/</span></span>
str <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//匹配字符串中是否含有除了a以外的字符串</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="正则-重复-n-n-m"><a href="#正则-重复-n-n-m" class="headerlink" title="正则: 重复 {n}  {n,m}"></a>正则: 重复 {n}  {n,m}</h5><blockquote>
<p>通过两次可以设置一个内容出现的次数</p>
<p>量词只对它前边的<code>一个内容</code>起作用</p>
<p>字符{n} 表示前边一个字符连续出现n次</p>
<p>如果是多个字符连续出现使用: <code>(字符串)</code>{n} 的形式匹配</p>
<p>如果不确定连续出现几次可以使用: <code>(字符串)</code>{n,m} 的形式匹配</p>
<p>语法: </p>
<p>​    var reg = /(字符串){n}/;</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(abc)&#123;2&#125;</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>    <span class="token comment">//匹配的是: abcabc</span>
<span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc&#123;2&#125;</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>      <span class="token comment">//匹配的是: abcc</span>
<span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc&#123;2,3&#125;</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>      <span class="token comment">//匹配的是: abcc 或 abccc</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h5 id="正则-至少一个-n"><a href="#正则-至少一个-n" class="headerlink" title="正则: 至少一个 n+"></a>正则: 至少一个 n+</h5><blockquote>
<p>匹配任何包含至少一个n的字符串 相当于 {1,}</p>
<p>语法:</p>
<p>​    var reg = /ab+c/;   //匹配abc,abbc等</p>
</blockquote>
<h5 id="正则-零个或多个-n"><a href="#正则-零个或多个-n" class="headerlink" title="正则: 零个或多个 n*"></a>正则: 零个或多个 n*</h5><blockquote>
<p>匹配任何包含零个或者多个n的字符串  相当于 {0,}</p>
<p>语法:</p>
<p>​    var reg = /ab*c/;   //匹配ac,abc,abbc等</p>
</blockquote>
<h5 id="正则-零个或一个-n"><a href="#正则-零个或一个-n" class="headerlink" title="正则: 零个或一个 n?"></a>正则: 零个或一个 n?</h5><blockquote>
<p>匹配任何包含零个或者个的字符串   相当于 {0,1}</p>
<p>语法:</p>
<p>​    var reg = /ab?c/;   //匹配ac,abc</p>
</blockquote>
<h5 id="正则-开头-n"><a href="#正则-开头-n" class="headerlink" title="正则:  开头 ^n"></a>正则:  开头 ^n</h5><blockquote>
<p>匹配以指定字符开头的字符串</p>
<p>语法:</p>
<p>​    var reg = /^a/;   //匹配ac,abc,abbc等以a开头的字符串</p>
</blockquote>
<h5 id="正则-结尾-n"><a href="#正则-结尾-n" class="headerlink" title="正则:  结尾 n$"></a>正则:  结尾 n$</h5><blockquote>
<p>匹配以指定字符结尾的字符串</p>
<p>如果在正则表达式中同时使用^和$则要求字符串必须完全符合正则表达式:</p>
<p>reg = /^a$/    //只能匹配到只有a的字符串</p>
<p>var reg = /abc$/;   //匹配c,ac,abc,abbc等以c结尾的字符串</p>
</blockquote>
<h5 id="正则：特殊字符"><a href="#正则：特殊字符" class="headerlink" title="正则：特殊字符"></a>正则：特殊字符</h5><blockquote>
<p>正则表达式中存在一些特殊意义的字符：</p>
<ul>
<li> .   -表示任意一个字符(除了换行分页)</li>
<li> \w   -表示任意字母，数字，下划线          [A-z0-9_]</li>
<li> \W  -表示除了任意字母，数字，下划线        [^A-z0-9_]</li>
<li> \d   -表示任意数字  [0-9]</li>
<li> \D   -表示除了数字     [^0-9]</li>
<li> \s   - 表示空格</li>
<li> \S   -表示除了空格</li>
<li> \b   -表示单词边界</li>
<li> \B   -表示除了单词边界</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\bcat\b </span><span class="token regex-delimiter">/</span></span>    <span class="token comment">// 检测有无cat这个单词</span>
reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">"this is a cats"</span><span class="token punctuation">)</span>    <span class="token comment">//没有cat，返回falst</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h3 id="16-Web-APIs"><a href="#16-Web-APIs" class="headerlink" title="16 Web APIs"></a>16 Web APIs</h3><blockquote>
<p>Web API是浏览器提供的一套浏览器功能和页面元素的API(DOM和BOM)</p>
<p>web API主要是针对浏览器提供的接口,主要针对浏览器做交互效果.</p>
<p>Web API一般都有输入和输出(函数的传参和返回值),Web API很多都是方法(函数)</p>
</blockquote>
<h3 id="17-对象-DOM"><a href="#17-对象-DOM" class="headerlink" title="17 对象(DOM)"></a>17 对象(DOM)</h3><h4 id="DOM"><a href="#DOM" class="headerlink" title="DOM"></a>DOM</h4><blockquote>
<ul>
<li>DOM,全称Document Object Model文档对象模型</li>
<li>js中通过DOM对html文档进行操作。</li>
<li>文档：   –文档表示的就是整个html网页文档,document表示</li>
<li>元素：   –页面的所有标签都是元素，使用element表示</li>
<li>节点：   –网页中的多有内容都是节点(标签，属性，文本，注释)，使用node表示。</li>
<li>对象：   –对象表示将网页中的每一个部分都转换为了一个对象</li>
<li>模型：    –使用模型来表示对象之间的关系，方便操纵对象</li>
</ul>
</blockquote>
<h5 id="节点"><a href="#节点" class="headerlink" title="节点"></a>节点</h5><blockquote>
<p>节点：Node–构成html文档最基本的单位</p>
<p>常用节点分为四类：</p>
<ol>
<li>文档节点：整个html文档</li>
<li>元素节点：html文档中的html标签</li>
<li>属性节点：元素的属性</li>
<li>文本节点：html标签中的文本内容，空白，换行</li>
</ol>
</blockquote>
<p>节点的属性：</p>
<table>
<thead>
<tr>
<th></th>
<th>node-Name</th>
<th>node-Type</th>
<th>node-Value</th>
</tr>
</thead>
<tbody><tr>
<td>文档节点</td>
<td>document</td>
<td>9</td>
<td>null</td>
</tr>
<tr>
<td>元素节点</td>
<td>标签名</td>
<td>1</td>
<td>null</td>
</tr>
<tr>
<td>属性节点</td>
<td>属性名</td>
<td>2</td>
<td>属性值</td>
</tr>
<tr>
<td>文本节点</td>
<td>text</td>
<td>3</td>
<td>文本内容</td>
</tr>
</tbody></table>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/buGCUH"><img src="https://s4.ax1x.com/2022/02/28/buGCUH.png" alt="buGCUH.png"></a></p>
<h5 id="事件"><a href="#事件" class="headerlink" title="事件"></a>事件</h5><blockquote>
<p>事件，就是文档或者浏览器窗口发生的一些特定的交互瞬间触发响应机制。</p>
<p>事件三要素： 事件源 事件类型 事件处理程序</p>
<p>javascript与html之间的交互式通过事件实现的</p>
<p>对应web应用来说，有下面这些代表性的事件：</p>
<p>​        点击某个元素，将鼠标移动到某个元素上方，按下键盘上的某个键……</p>
<p>整个文档按照自上向下的顺序执行，如果js代码写在上面，很有可能会读取不到相应的对象。可以使用 window中的onload事件来控制js代码在页面加载完再运行</p>
<ul>
<li>当时间的响应函数被触发时，浏览器每次都会将一个事件对象作为实参传递进响应函数，事件对象中封装这当前事件相关的一切信息，比如：鼠标的坐标，键盘哪个按键被按下。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//给window绑定onload事件，只有页面加载完成之后才会运行相应的js代码，用于解决js代码在页面加载之前运行的问题</span>
window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    js代码 
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>btn_Id<span class="token punctuation">"</span></span> <span class="token special-attr"><span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>这是一个按钮<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
      	<span class="token comment">//获取事件源 --> 注册事件 -->添加事件处理程序</span>
        <span class="token comment">//获取id属性为btn_Id的标签对象</span>
        <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn_Id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">//设置对应对象的单击事件函数</span>
        btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"点击成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<table>
<thead>
<tr>
<th>属性</th>
<th>此事件发生在何时…</th>
</tr>
</thead>
<tbody><tr>
<td>onabort</td>
<td>图像的加载被中断。</td>
</tr>
<tr>
<td>onblur</td>
<td>元素失去焦点。</td>
</tr>
<tr>
<td>onchange</td>
<td>域的内容被改变。</td>
</tr>
<tr>
<td>onclick</td>
<td>当用户点击某个对象时调用的事件句柄。</td>
</tr>
<tr>
<td>ondblclick</td>
<td>当用户双击某个对象时调用的事件句柄。</td>
</tr>
<tr>
<td>onerror</td>
<td>在加载文档或图像时发生错误。</td>
</tr>
<tr>
<td>onfocus</td>
<td>元素获得焦点。</td>
</tr>
<tr>
<td>onkeydown</td>
<td>某个键盘按键被按下。</td>
</tr>
<tr>
<td>onkeypress</td>
<td>某个键盘按键被按下并松开。</td>
</tr>
<tr>
<td>onkeyup</td>
<td>某个键盘按键被松开。</td>
</tr>
<tr>
<td>onsubmit</td>
<td>加载页面确认按钮被点击。</td>
</tr>
<tr>
<td>onmousedown</td>
<td>鼠标按钮被按下。</td>
</tr>
<tr>
<td>onmousemove</td>
<td>鼠标被移动。</td>
</tr>
<tr>
<td>onmouseout</td>
<td>鼠标从某元素移开。</td>
</tr>
<tr>
<td>onmouseover</td>
<td>鼠标移到某元素之上。</td>
</tr>
<tr>
<td>onmouseup</td>
<td>鼠标按键被松开。</td>
</tr>
<tr>
<td>onreset</td>
<td>重置按钮被点击。</td>
</tr>
<tr>
<td>onresize</td>
<td>窗口或框架被重新调整大小。</td>
</tr>
<tr>
<td>onselect</td>
<td>文本被选中。</td>
</tr>
<tr>
<td>onload</td>
<td>重新加载页面</td>
</tr>
<tr>
<td>onunload</td>
<td>用户退出页面。</td>
</tr>
<tr>
<td>onpageshow</td>
<td>重新加载页面</td>
</tr>
<tr>
<td>onscroll</td>
<td>滚动事件</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>altKey</td>
<td>返回当事件被触发时，”ALT” 是否被按下。</td>
</tr>
<tr>
<td>button</td>
<td>返回当事件被触发时，哪个鼠标按钮被点击。</td>
</tr>
<tr>
<td>clientX</td>
<td>返回当事件被触发时，鼠标指针的水平坐标。</td>
</tr>
<tr>
<td>clientY</td>
<td>返回当事件被触发时，鼠标指针的垂直坐标。</td>
</tr>
<tr>
<td>ctrlKey</td>
<td>返回当事件被触发时，”CTRL” 键是否被按下。</td>
</tr>
<tr>
<td>metaKey</td>
<td>返回当事件被触发时，”meta” 键是否被按下。</td>
</tr>
<tr>
<td>relatedTarget</td>
<td>返回与事件的目标节点相关的节点。</td>
</tr>
<tr>
<td>screenX</td>
<td>返回当某个事件被触发时，鼠标指针的水平坐标。</td>
</tr>
<tr>
<td>screenY</td>
<td>返回当某个事件被触发时，鼠标指针的垂直坐标。</td>
</tr>
<tr>
<td>shiftKey</td>
<td>返回当事件被触发时，”SHIFT” 键是否被按下。</td>
</tr>
</tbody></table>
<h5 id="DOM的属性"><a href="#DOM的属性" class="headerlink" title="DOM的属性"></a>DOM的属性</h5><blockquote>
<p>元素.innerHTML     获取元素内部的html代码(包括html标签)</p>
<p>对于自结束标签，此属性没有意义</p>
</blockquote>
<blockquote>
<p>元素.属性    获取元素的对应属性</p>
<p>​    对于class属性需要使用 元素.className来读取，其他方式无法读取</p>
</blockquote>
<blockquote>
<p>元素.innerText</p>
<p>该属性可以获取到元素内部的文本内容，不识别html标签，是非w3c标准的。</p>
<p>和innerHTML类似，不同的是它会自动将html标签去除，只获取其中的文本</p>
</blockquote>
<blockquote>
<p>document.body</p>
<p>该属性保存着文档中body标签的引用</p>
<p>语法：</p>
<p>​    var body = document.body;</p>
</blockquote>
<blockquote>
<p>document.documentElement</p>
<p>该属性保存着html的根标签</p>
<p>语法：</p>
<p>​    var html = document.documentElement;</p>
</blockquote>
<blockquote>
<p>document.all</p>
<p>该属性保存着文档中全部的标签，等于：  document.getElementTagName(“*”);</p>
<p>语法：</p>
<p>  var all = document.all;</p>
</blockquote>
<h5 id="表单属性"><a href="#表单属性" class="headerlink" title="表单属性"></a>表单属性</h5><blockquote>
<p>先需要获取对应的表单元素。</p>
<p>type,value,checked,selected.disabled(被禁用)</p>
</blockquote>
<h5 id="DOM的方法"><a href="#DOM的方法" class="headerlink" title="DOM的方法"></a>DOM的方法</h5><blockquote>
<p>浏览器已经提供了文档的节点对象: document</p>
<p>可以在页面中直接使用，文档节点代表的是整个网页  </p>
<p><code>核心思想：  获取对象  -》  操作对象</code>  </p>
</blockquote>
<p>举例：</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>btn_id<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>这是一个按钮<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
        <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn_id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//通过文档对象的的id名来获取对应的标签对象，更改标签对象的内容。</span>
        btn<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">"Button"</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h6 id="getElementById"><a href="#getElementById" class="headerlink" title="getElementById()"></a>getElementById()</h6><blockquote>
<p>通过ID属性获取一个元素节点对象(id属性是唯一的)</p>
<p>返回的是一个封装好的对象，需要使用属性值来提取或者修改内容</p>
<p>如果未找到返回null</p>
<p>语法：</p>
<p>​    var 节点对象 = document.getElementById(“id属性”);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> ele <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="getElementsByTagName"><a href="#getElementsByTagName" class="headerlink" title="getElementsByTagName()"></a>getElementsByTagName()</h6><blockquote>
<p>通过标签名获取<code>一组</code>元素节点对象(相同的标签名会有多个节点)</p>
<p>这个方法会返回一个==类数组对象==，所有查询到的元素都会封装到对象中去</p>
<p>即使查询到的元素只有一个也会封装到数组中</p>
<p>使用document.getElementsByTagName(“标签名”)；会获得当前页面上所有的指定标签名，封装成一个伪数组的形式返回。</p>
<p>语法：</p>
<p>​    var 节点对象 = document.getElementsByTagName(“标签名”)；</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> ele <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">"标签名"</span><span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<blockquote>
<p>可以使用父元素调用getElementsByTagName(“标签名”)来获取父元素内部的指定标签对象。</p>
<p>==但是父元素必须是单个对象，不能是父元素数组==</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> ol <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">"ol"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> li <span class="token operator">=</span> ol<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">"li"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">dir</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
</blockquote>
<h6 id="getElementsByName"><a href="#getElementsByName" class="headerlink" title="getElementsByName()"></a>getElementsByName()</h6><blockquote>
<p>通过name属性获取<code>一组</code>元素节点对象(相同的name属性会有多个节点)</p>
<p>这个方法会返回一个==类数组对象==，所有查询到的元素都会封装到对象中去</p>
<p>即使查询到的元素只有一个也会封装到数组中</p>
<p>属性： length    innerHTML</p>
<p>语法：</p>
<p>​    var 节点对象 = document.getElementsByName(“name属性”);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> ele <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">"标签名"</span><span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h6 id="getElementsByClassName"><a href="#getElementsByClassName" class="headerlink" title="getElementsByClassName()"></a>getElementsByClassName()</h6><blockquote>
<p>getElementsByClassName()是html5新增的方法</p>
</blockquote>
<blockquote>
<p>通过class属性获取<code>一组</code>元素节点对象(相同的class属性会有多个节点)</p>
<p>这个方法会返回一个==类数组对象==，所有查询到的元素都会封装到对象中去</p>
<p>此属性IE8及以下版本不支持</p>
<p>即使查询到的元素只有一个也会封装到数组中</p>
<p>语法：</p>
<p>​    var 节点对象 = document.getElementsByClassName(“class属性”);</p>
</blockquote>
<h6 id="querySelector"><a href="#querySelector" class="headerlink" title="querySelector()"></a>querySelector()</h6><blockquote>
<p>通过css选择器来查询一个元素节点对象,是html5新增的对象</p>
<p>==只会返回一个元素节点对象==，如果有多个符合要求的节点对象，<code>也只会返回第一个</code></p>
<p>在IE8中也可以使用</p>
<p>语法：</p>
<p>​    document.querySelector(“选择器”)；</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"div .class"</span><span class="token punctuation">)</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>


</blockquote>
<h6 id="querySelectorAll"><a href="#querySelectorAll" class="headerlink" title="querySelectorAll()"></a>querySelectorAll()</h6><blockquote>
<p>通过css选择器来查询多个元素节点对象,是html5新增的对象</p>
<p>这个方法会返回一个==类数组对象==，所有查询到的元素都会封装到对象中去</p>
<p>即使查询到的元素只有一个也会封装到数组中</p>
<p>语法：</p>
<p>​    document.querySelectorAll(“css选择器”)；</p>
</blockquote>
<h4 id="获取子节点方法和属性"><a href="#获取子节点方法和属性" class="headerlink" title="获取子节点方法和属性"></a>获取子节点方法和属性</h4><h5 id="属性："><a href="#属性：" class="headerlink" title="属性："></a>属性：</h5><blockquote>
<p>使用具体的元素节点调用</p>
</blockquote>
<h6 id="childNodes"><a href="#childNodes" class="headerlink" title="childNodes"></a>childNodes</h6><blockquote>
<p>表示当前节点的<code>所有子节点</code>，返回一个数组。</p>
<p>会获取包括文本节点在内的所有节点，不光是元素节点(换行居然也算!在IE8及以下版本不会将空白当成子节点)</p>
<p>语法：</p>
<p>​    var cns = 子节点对象.childNodes;</p>
</blockquote>
<h6 id="children"><a href="#children" class="headerlink" title="children"></a>children</h6><blockquote>
<p>表示当前节点的所有子元素节点(仅仅是元素节点，不包含文本节点)</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.children;</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">	<span class="token comment">//获取指定所有节点中的元素节点</span>
<span class="token keyword">var</span> dou <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByClassName</span><span class="token punctuation">(</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>dou<span class="token punctuation">.</span>childNodes<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>dou<span class="token punctuation">.</span>childNodes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>nodeType <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ul<span class="token punctuation">.</span>childNodes<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</blockquote>
<h6 id="firstChild"><a href="#firstChild" class="headerlink" title="firstChild"></a>firstChild</h6><blockquote>
<p>表示当前节点的==第一个子节点(包括空白文本节点)==</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.firstChild;</p>
</blockquote>
<h6 id="firstElementChild"><a href="#firstElementChild" class="headerlink" title="firstElementChild"></a>firstElementChild</h6><blockquote>
<p>获取当前元素的==第一个子元素(只有元素节点)==</p>
<p>==不支持IE8及以下的浏览器==</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.firstElementChild;</p>
</blockquote>
<h6 id="lastChild"><a href="#lastChild" class="headerlink" title="lastChild"></a>lastChild</h6><blockquote>
<p>表示当前节点的最后一个子节点(包括空白文本节点)</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.lastChild;</p>
</blockquote>
<h6 id="lastElementChild"><a href="#lastElementChild" class="headerlink" title="lastElementChild"></a>lastElementChild</h6><blockquote>
<p>获取当前元素的==最后一个子元素(只有元素节点)==</p>
<p>==不支持IE8及以下的浏览器==</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.lastElementChild;</p>
</blockquote>
<h5 id="方法："><a href="#方法：" class="headerlink" title="方法："></a>方法：</h5><blockquote>
<p>通过具体的元素节点调用</p>
</blockquote>
<h6 id="getElementsByTagName-1"><a href="#getElementsByTagName-1" class="headerlink" title="getElementsByTagName()"></a>getElementsByTagName()</h6><blockquote>
<p>返回当前节点的指定标签名后代节点</p>
<p>语法：</p>
<p>var ebt = 元素节点.getElementsByTagName(“标签名”);</p>
</blockquote>
<h4 id="获取父亲-兄弟节点的方法和属性"><a href="#获取父亲-兄弟节点的方法和属性" class="headerlink" title="获取父亲,兄弟节点的方法和属性"></a>获取父亲,兄弟节点的方法和属性</h4><h5 id="属性：-1"><a href="#属性：-1" class="headerlink" title="属性："></a>属性：</h5><h6 id="parentNode"><a href="#parentNode" class="headerlink" title="parentNode"></a>parentNode</h6><blockquote>
<p>获取当前节点的父节点(元素最近的父节点)</p>
<p>==因为获取的是元素节点的父节点，所以获得是标签节点，不会是文本节点。==</p>
<p>如果未找到返回null。</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.parentNode;</p>
</blockquote>
<h6 id="previousSibling"><a href="#previousSibling" class="headerlink" title="previousSibling"></a>previousSibling</h6><blockquote>
<p>获取当前节点前一个兄弟节点(会获取空白的文本)</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.previousSibling;</p>
</blockquote>
<h6 id="previousElementSibling"><a href="#previousElementSibling" class="headerlink" title="previousElementSibling"></a>previousElementSibling</h6><blockquote>
<p>获取当前节点前一个兄弟元素(不会获取到文本元素)</p>
<p>==不支持IE8及以下的浏览器==</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.previousElementSibling;</p>
</blockquote>
<h6 id="nextSibling"><a href="#nextSibling" class="headerlink" title="nextSibling"></a>nextSibling</h6><blockquote>
<p>获取当前节点后一个兄弟节点(会获取空白的文本)</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.nextSibling;</p>
</blockquote>
<h6 id="nextElementSibling"><a href="#nextElementSibling" class="headerlink" title="nextElementSibling"></a>nextElementSibling</h6><blockquote>
<p>获取当前节点前一个兄弟元素节点(不会获取到文本元素)</p>
<p>==不支持IE8及以下的浏览器==</p>
<p>语法：</p>
<p>​    var cns = 元素节点对象.nextElementSibling;</p>
</blockquote>
<h3 id="18-操作对象"><a href="#18-操作对象" class="headerlink" title="18 操作对象"></a>18 操作对象</h3><h4 id="1-创建方法"><a href="#1-创建方法" class="headerlink" title="1.创建方法"></a>1.创建方法</h4><h5 id="innerHtml"><a href="#innerHtml" class="headerlink" title="innerHtml"></a>innerHtml</h5><blockquote>
<p>向指定元素中添加html语言。</p>
<p>如果创建多个元素时，==采取拼接效率会比较低。如果采用数组形式拼接，效率会很高。==</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> ele<span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ele<span class="token punctuation">.</span>innerHtml <span class="token operator">=</span> <span class="token string">"&lt;a>&lt;/a>"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
</blockquote>
<h5 id="write"><a href="#write" class="headerlink" title="write()"></a>write()</h5><blockquote>
<p>创建元素，如果页面文档流加载完毕。再调用这句话，会导致页面重新绘制。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">document<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"&lt;a>&lt;/a>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
</blockquote>
<h5 id="createElement"><a href="#createElement" class="headerlink" title="createElement()"></a>createElement()</h5><blockquote>
<p>创建一个元素节点对象</p>
<p>需要一个标签名作为参数，会返回该标签名创建元素节点的对象.</p>
<p>如果创建多个元素时，效率会比较高。</p>
<p>语法：</p>
<p>​    var ele= document.createElement(“标签名”)</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//创建一个div标签对象</span>
<span class="token keyword">var</span> ele<span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h5 id="createTextNode"><a href="#createTextNode" class="headerlink" title="createTextNode()"></a>createTextNode()</h5><blockquote>
<p>创建一个文本节点对象</p>
<p>需要一个文本内容作为参数，会将根据该文本内容创建的节点返回</p>
<p>语法：</p>
<p>var text = createTextNode(“文本”);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//创建一个文本对象</span>
<span class="token keyword">var</span> text<span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createTextNode</span><span class="token punctuation">(</span><span class="token string">"文本"</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>





<h4 id="2-添加方法"><a href="#2-添加方法" class="headerlink" title="2.添加方法"></a>2.添加方法</h4><h5 id="appendChild"><a href="#appendChild" class="headerlink" title="appendChild()"></a>appendChild()</h5><blockquote>
<p>向一个父节点添加子节点，如果已经有元素则在后面追加。</p>
<p>语法：</p>
<p>​    父节点.appendChild(子节点)；</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//创立一个元素节点</span>
<span class="token keyword">var</span> ele<span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//创立一个文本节点</span>
<span class="token keyword">var</span> text<span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createTextNode</span><span class="token punctuation">(</span><span class="token string">"文本"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//将文本节点添加到元素节点中</span>
ele<span class="token punctuation">.</span><span class="token function">appendChild</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="insertBefore"><a href="#insertBefore" class="headerlink" title="insertBefore()"></a>insertBefore()</h5><blockquote>
<p>可以在指定的子节点前插入新的字节点</p>
<p>语法：</p>
<p>​    父节点.insertBefore(新节点,旧节点);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">div<span class="token punctuation">.</span><span class="token function">insertBefore</span><span class="token punctuation">(</span>newEle<span class="token punctuation">,</span>oldEle<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h4 id="3-替换方法"><a href="#3-替换方法" class="headerlink" title="3.替换方法"></a>3.替换方法</h4><h5 id="replaceChlid"><a href="#replaceChlid" class="headerlink" title="replaceChlid()"></a>replaceChlid()</h5><blockquote>
<p>可以使用指定的新节点替换旧节点</p>
<p>语法：</p>
<p>​    父节点.replaceChild(新节点,旧节点)；</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">div<span class="token punctuation">.</span><span class="token function">replaceChild</span><span class="token punctuation">(</span>newEle<span class="token punctuation">,</span>oldEle<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h4 id="4-删除方法"><a href="#4-删除方法" class="headerlink" title="4.删除方法"></a>4.删除方法</h4><h5 id="removeChild"><a href="#removeChild" class="headerlink" title="removeChild()"></a>removeChild()</h5><blockquote>
<p>可以删除一个子节点</p>
<p>语法：</p>
<p>​    父节点.removeChild(子节点)；</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">div<span class="token punctuation">.</span><span class="token function">removeChild</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="5-复制节点-克隆"><a href="#5-复制节点-克隆" class="headerlink" title="5.复制节点(克隆)"></a>5.复制节点(克隆)</h4><h5 id="cloneNode"><a href="#cloneNode" class="headerlink" title="cloneNode()"></a>cloneNode()</h5><blockquote>
<p>调用该方法返回指定节点的一个副本</p>
<p>语法：</p>
<p>​    节点.cloneNode()   //浅拷贝  只会拷贝标签不会拷贝内容</p>
<p>​    节点.cloneNode(true)   //深拷贝  会拷贝标签和内容</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> li <span class="token operator">=</span> li<span class="token punctuation">.</span><span class="token function">cloneNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="6-取消默认行为"><a href="#6-取消默认行为" class="headerlink" title="6.取消默认行为"></a>6.取消默认行为</h4><blockquote>
<p>提交按钮和超链接会又默认的跳转行为，有时候需要取消。</p>
<p>有时需要取消页面上的默认事件，如复制，上下文菜单。</p>
<p>取消方法：</p>
<p>​    在相应标签绑定的函数最后添加代码：</p>
<p>​            return ：falst；</p>
<p>​    向helf属性中添加：</p>
<p>​            javascript:; </p>
</blockquote>
<h3 id="19-操作对象的样式和属性"><a href="#19-操作对象的样式和属性" class="headerlink" title="19. 操作对象的样式和属性"></a>19. 操作对象的样式和属性</h3><h4 id="1-操作内联样式"><a href="#1-操作内联样式" class="headerlink" title="1.操作内联样式"></a>1.操作内联样式</h4><blockquote>
<p>通过js修改元素的样式：</p>
<p>语法： 元素.style.样式名 = 样式值;</p>
<p>注意：如果css的样式名中含有 - ，这种名称在接js中不合法，比如：background-color ，需要将这种样式名修改为驼峰命名法，去掉-，然后将-后的字母大写。</p>
<p>background-color   -》  backgroundColor</p>
<p>通过style属性设置的样式是修饰的内联样式，==如果在非内联样式中样式后指定 !important属性，不会被js代码所改变。==</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> div <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"相应id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//通过id获取指定的节点</span>
div<span class="token punctuation">.</span>style<span class="token punctuation">.</span>width <span class="token operator">=</span> <span class="token string">"22px"</span><span class="token punctuation">;</span>     <span class="token comment">//通过js修改相应元素的样式</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h4 id="2-读取内联样式"><a href="#2-读取内联样式" class="headerlink" title="2.读取内联样式"></a>2.读取内联样式</h4><blockquote>
<p>通过js读取元素的样式：</p>
<p>​    语法： 元素.style.样式名;</p>
<p>注意：如果css的样式名中含有 - ，这种名称在接js中不合法，比如：background-color ，需要将这种样式名修改为驼峰命名法，去掉-，然后将-后的字母大写。</p>
<p>读取到的***px是字符串，如果直接与数字相加是拼串操作。</p>
<p>通过style属性读取的是内联属性。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> div <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"相应id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//通过id获取指定的节点</span>
<span class="token function">alert</span><span class="token punctuation">(</span>div<span class="token punctuation">.</span>style<span class="token punctuation">.</span>width<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//通过js读取相应元素的样式</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h4 id="3-读取当前样式-currentStyle"><a href="#3-读取当前样式-currentStyle" class="headerlink" title="3.读取当前样式(currentStyle)"></a>3.读取当前样式(currentStyle)</h4><blockquote>
<p><code>（只有ie浏览器可以使用！！非常恶心！！）</code></p>
<p>通过js读取档期啊元素显示的样式：</p>
<p>​    语法： 元素.currentStyle.样式名;</p>
<p>可以读取当前元素正在显示的样式，不论内联还是外联。</p>
<p>如果当前元素没有设置指定读取的值，会返回默认的值。</p>
<p>==获取到的属性是只读的，想更改需要使用元素名.style.属性名 = 指定值的形式修改。==</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> div <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"相应id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//通过id获取指定的节点</span>
<span class="token keyword">var</span> css <span class="token operator">=</span> div<span class="token punctuation">.</span>currentStyle<span class="token punctuation">.</span>width<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<h4 id="4-读取当前样式-其他浏览器"><a href="#4-读取当前样式-其他浏览器" class="headerlink" title="4.读取当前样式(其他浏览器)"></a>4.读取当前样式(其他浏览器)</h4><blockquote>
<p>所有浏览器中都可以使用(不支持Ie8及以下浏览器)：</p>
<p>​        getComputedStyle()这个方法来获取元素的当前样式</p>
<p>​        这个方法是window的方法可以直接使用</p>
<p>需要两个参数：</p>
<p>​    var 返回元素名 = getComputedStyle(节点,null);</p>
<p>​        第一个：要获取样式的元素</p>
<p>​        第二个：可以传递一个伪元素，一般传递null</p>
<p>​    该方法会返回一个对象，<code>对象中封装了当前元素的对应的样式</code>，可以使用返回元素.样式的形式访问需要的样式。</p>
<p>如果获取的样式没有设置，则会获取到真实的值，而不是默认值。</p>
<p>​    比如:  ==没有设置width，不会获取到auto，而是会获取到一个真实的长度。==</p>
<ul>
<li>返回的值会带着相应的单位，类型为字符串。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> div <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"相应id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//通过id获取指定的节点</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">getComputedStyle</span><span class="token punctuation">(</span>div<span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//获取指定节点的样式对象</span>
<span class="token function">alert</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>width<span class="token punctuation">)</span>   <span class="token comment">//从样式对象中获取样式</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>



<h4 id="5-读取当前样式-通用方法"><a href="#5-读取当前样式-通用方法" class="headerlink" title="5.读取当前样式(通用方法)"></a>5.读取当前样式(通用方法)</h4><blockquote>
<p>​    未有系统定义的通用方法，可以采取自己写函数的形式定义通用方法：</p>
<p>​    ie8及以下版本浏览器无getComputedStyle属性，其他浏览器存在getComputedStyle属性为关键条件。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">getStyle</span><span class="token punctuation">(</span> <span class="token parameter">node <span class="token punctuation">,</span> name</span> <span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>window<span class="token punctuation">.</span>getComputedStyle<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>  <span class="token comment">//判断是否存在属性,ie8中无次属性。</span>
		<span class="token keyword">return</span> <span class="token function">getComputedStyle</span><span class="token punctuation">(</span>node<span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
				<span class="token punctuation">&#125;</span>
    	<span class="token keyword">else</span><span class="token punctuation">&#123;</span>
            	<span class="token keyword">return</span> node<span class="token punctuation">.</span>currentStyle<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="6-其他操作样式的方法"><a href="#6-其他操作样式的方法" class="headerlink" title="6.其他操作样式的方法"></a>6.其他操作样式的方法</h4><blockquote>
<p>clientWidth 返回当前对象的可见宽度(盒子的宽度和内容的宽度和);</p>
<p>clientHeight 返回当前对象的可见高度(盒子的高度和内容的高度和);</p>
<ul>
<li>返回的是一个数字，并不是一个字符串。可以直接进行计算。</li>
<li>因为返回的是<code>内容区和内边距</code>，所以只读。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> box1 <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"box1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">alert</span><span class="token punctuation">(</span>box1<span class="token punctuation">.</span>clientWidth<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">/*返回对象的可见宽度*/</span>
<span class="token function">alert</span><span class="token punctuation">(</span>box1<span class="token punctuation">.</span>clientHeight<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">/*返回对象的可见高度*/</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<blockquote>
<p>offsetWidth: 返回当前对象整个盒子的宽度(盒子整体的宽度);</p>
<p>offsetHeight: 返回当前对象整个盒子的高度(盒子整体的高度);</p>
<ul>
<li>返回的是一个数字，并不是一个字符串。可以直接进行计算。</li>
<li>因为返回的是<code>内容区，内边距和边框</code>，所以只读。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> box1 <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"box1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">alert</span><span class="token punctuation">(</span>box1<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">/*返回对象的全部宽度*/</span>
<span class="token function">alert</span><span class="token punctuation">(</span>box1<span class="token punctuation">.</span>offsetHeight<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">/*返回对象的全部高度*/</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<blockquote>
<ul>
<li>scrollWidth   </li>
<li>scrollHeight<ul>
<li>可以获取整个滚动区域的宽度和高度。</li>
</ul>
</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>scrollLeft</li>
<li>scrollTop<ul>
<li>可以获取滚动条的距离。</li>
</ul>
</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li>scrollHeight - scrollTop == clientHeight</li>
</ul>
<p>当以上等式满足时，表示滚动条到底。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> box1 <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"box1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">/*onscroll事件会在滚动条滚动时触发*/</span>
box1<span class="token punctuation">.</span><span class="token function-variable function">onscroll</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>box1<span class="token punctuation">.</span>scrollHeight <span class="token operator">-</span> box1<span class="token punctuation">.</span>scrollTop <span class="token operator">==</span> box1<span class="token punctuation">.</span>clientHeight<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    	<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"滚动条已经滚动到底"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="修改类的方式修改样式"><a href="#修改类的方式修改样式" class="headerlink" title="修改类的方式修改样式"></a>修改类的方式修改样式</h4><blockquote>
<p>每使用一次js修改元素属性的代码，浏览器就会重新渲染一次页面。</p>
<p>修改大量的样式时需要大量的代码。</p>
<ul>
<li>可以通过提前定义css类样式，在修改元素的类的方法来间接的修改元素的样式。</li>
<li>只需要修改一次，就可以修改多个样式。浏览器只需要渲染一次，性能较好。</li>
<li>使行为和表现分离。</li>
</ul>
</blockquote>
<ul>
<li>更改类属性</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn01"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//btn的类名b1</span>
            <span class="token comment">//修改btn的类名为b2</span>
            btn<span class="token punctuation">.</span>className <span class="token operator">=</span> <span class="token string">"b2"</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<ul>
<li>增加类属性</li>
</ul>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn01"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//btn的类名b1</span>
            <span class="token comment">//修改btn的类名为b2 b1</span>
            btn<span class="token punctuation">.</span>className <span class="token operator">+=</span> <span class="token string">" b2"</span><span class="token punctuation">;</span>   <span class="token comment">//中间需要一个空格</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="获取属性"><a href="#获取属性" class="headerlink" title="获取属性"></a>获取属性</h4><blockquote>
<p>可以通过获得要操作的对象，然后通过两种方式获取对象中的属性。</p>
<p>既可以获得原有属性也可以获得默认属性。</p>
<p>==自定义属性，用于保存一定的数据。==</p>
<p>自定义属性规范： 以 data-属性名</p>
<ol>
<li>对象.属性名；</li>
<li>对象.getAttribute(‘属性名’);</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">var</span> db <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
document<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>db<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
document<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>db<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ul>
<li>可以向标签元素中添加自定义属性。</li>
<li>对象.属性名 方式只能获得对象的内置属性(元素本身自定义的属性)。</li>
<li>对象,getAttribute(“属性”);可以获得对象的自定意属性。</li>
</ul>
</blockquote>
<h4 id="获取自定义属性"><a href="#获取自定义属性" class="headerlink" title="获取自定义属性"></a>获取自定义属性</h4><blockquote>
<p>可以获取标签中的用户自定义属性有两种方法：</p>
<ol>
<li>对象.getAttribute(‘属性名’);   /任意自定义属性都可以获取</li>
<li>对象.dataset.自定义属性名；   //只有符合h5新的自定义属性规范的才能获取到。</li>
<li>对象.dataset是一个集合，里面存放了所有以data开头的自定义属性。对象.dataset[“自定义属性名”]；也可以拿到。</li>
<li>如过自定义属性中存在多个 - 链接：<ol>
<li>对象.getAttribute(‘属性名’); </li>
<li>对象.dataset.自定义属性名；   //使用驼峰命名法</li>
<li>对象.dataset[“自定义属性名”]；    //使用驼峰命名法</li>
</ol>
</li>
</ol>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token operator">&lt;</span>div data<span class="token operator">-</span>index<span class="token operator">=</span><span class="token string">"1"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>div data<span class="token operator">-</span>index<span class="token operator">-</span>time<span class="token operator">=</span><span class="token string">"1"</span><span class="token operator">></span>
  
<span class="token keyword">var</span> db <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelectorAll</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> db1 <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelectorAll</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> index1 <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">'data-index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> index2 <span class="token operator">=</span> db<span class="token punctuation">.</span>dataset<span class="token punctuation">.</span>index<span class="token punctuation">;</span>   <span class="token comment">//注意获取的是dataset集合中的值，必须符合h5规范才能使用，且注意属性名的区别。</span>

<span class="token keyword">var</span> index3 <span class="token operator">=</span> db1<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">'data-index-time'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> index4 <span class="token operator">=</span> db1<span class="token punctuation">.</span>dataset<span class="token punctuation">.</span>indexTime<span class="token punctuation">;</span>   <span class="token comment">//需要使用驼峰命名法</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


</blockquote>
<h4 id="设置属性"><a href="#设置属性" class="headerlink" title="设置属性"></a>设置属性</h4><blockquote>
<p>可以通过获得要操作的对象，然后通过两种方式更改对象中的属性。</p>
<ol>
<li>对象.属性 = ’值‘；  </li>
<li>对象.setAttribute(‘属性’,’值’);</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">var</span> db <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
db<span class="token punctuation">.</span>id <span class="token operator">=</span> <span class="token char">'db'</span><span class="token punctuation">;</span>
db<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token char">'db'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ul>
<li>对象.属性 = ’值‘；的方式只能设置内置属性值。</li>
<li>对象.setAttribute(‘属性’,’值’);可以设置自定义属性值。</li>
<li>当设置class属性时，采用对象.属性 = ’值‘；的方法比较特殊：</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">db<span class="token punctuation">.</span>className <span class="token operator">=</span> <span class="token char">'db'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
</blockquote>
<h4 id="移除属性"><a href="#移除属性" class="headerlink" title="移除属性"></a>移除属性</h4><blockquote>
<p>可以通过获得要操作的对象，然后removeAttribute()对象中的属性。</p>
<ul>
<li>removeAttribute(‘属性’);</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">var</span> db <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"doubao"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
db<span class="token punctuation">.</span><span class="token function">removeAttribute</span><span class="token punctuation">(</span><span class="token char">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<ul>
<li>使用removeAttribute()方法可以移除内置对象和自定义对象。</li>
</ul>
</blockquote>
<hr>
<h3 id="20-事件"><a href="#20-事件" class="headerlink" title="20. 事件"></a>20. 事件</h3><blockquote>
<p>事件，就是文档或者浏览器窗口发生的一些特定的交互瞬间</p>
<p>javascript与html之间的交互式通过事件实现的</p>
<p>对应web应用来说，有下面这些代表性的事件：</p>
<pre><code>    点击某个元素，将鼠标移动到某个元素上方，按下键盘上的某个键……
</code></pre>
<p>​    整个文档按照自上向下的顺序执行，如果js代码写在上面，很有可能会读取不到相应的对象。可以使用 window中的onload事件来控制js代码在页面加载完再运行</p>
<ul>
<li>当时间的响应函数被触发时，浏览器每次都会将一个事件对象作为实参传递进响应函数，事件对象中封装这当前事件相关的一切信息.<ul>
<li>比如：鼠标的坐标，键盘哪个按键被按下,鼠标滚轮滚动的方向。</li>
</ul>
</li>
<li>取消正在生效的事件只需要将事件函数赋值为 null 即可。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//给window绑定onload事件，只有页面加载完成之后才会运行相应的js代码，用于解决js代码在页面加载之前运行的问题</span>
window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    js代码 
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>btn_Id<span class="token punctuation">"</span></span> <span class="token special-attr"><span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>这是一个按钮<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript">
        <span class="token comment">//获取id属性为btn_Id的标签对象</span>
        <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn_Id"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">//设置对应对象的单击事件函数</span>
        btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"点击成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<table>
<thead>
<tr>
<th>属性</th>
<th>此事件发生在何时…</th>
</tr>
</thead>
<tbody><tr>
<td>onabort</td>
<td>图像的加载被中断。</td>
</tr>
<tr>
<td>onblur</td>
<td>元素失去焦点。</td>
</tr>
<tr>
<td>onchange</td>
<td>域的内容被改变。</td>
</tr>
<tr>
<td>onclick</td>
<td>当用户点击某个对象时调用的事件句柄。</td>
</tr>
<tr>
<td>ondblclick</td>
<td>当用户双击某个对象时调用的事件句柄。</td>
</tr>
<tr>
<td>onerror</td>
<td>在加载文档或图像时发生错误。</td>
</tr>
<tr>
<td>onfocus</td>
<td>元素获得焦点。</td>
</tr>
<tr>
<td>onkeydown</td>
<td>某个键盘按键被按下。</td>
</tr>
<tr>
<td>onkeypress</td>
<td>某个键盘按键被按下并松开。</td>
</tr>
<tr>
<td>onkeyup</td>
<td>某个键盘按键被松开。</td>
</tr>
<tr>
<td>onsubmit</td>
<td>加载页面确认按钮被点击。</td>
</tr>
<tr>
<td>onmousedown</td>
<td>鼠标按钮被按下。</td>
</tr>
<tr>
<td>onmousemove</td>
<td>鼠标被移动。</td>
</tr>
<tr>
<td>onmouseout</td>
<td>鼠标从某元素移开。(会冒泡)</td>
</tr>
<tr>
<td>onmouseleave</td>
<td>鼠标从某元素移开。(不会冒泡)</td>
</tr>
<tr>
<td>onmouseover</td>
<td>鼠标进入元素(会冒泡)</td>
</tr>
<tr>
<td>onmouseenter</td>
<td>鼠标进入元素(不会冒泡)）</td>
</tr>
<tr>
<td>onmouseup</td>
<td>鼠标按键被松开。</td>
</tr>
<tr>
<td>onreset</td>
<td>重置按钮被点击。</td>
</tr>
<tr>
<td>onresize</td>
<td>窗口或框架被重新调整大小。</td>
</tr>
<tr>
<td>onselect</td>
<td>文本被选中。</td>
</tr>
<tr>
<td>onload</td>
<td>重新加载页面</td>
</tr>
<tr>
<td>onunload</td>
<td>用户退出页面。</td>
</tr>
<tr>
<td>onpageshow</td>
<td>重新加载页面</td>
</tr>
<tr>
<td>onscroll</td>
<td>滚动事件</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>altKey</td>
<td>返回当事件被触发时，”ALT” 是否被按下。</td>
</tr>
<tr>
<td>button</td>
<td>返回当事件被触发时，哪个鼠标按钮被点击。</td>
</tr>
<tr>
<td>clientX</td>
<td>返回当事件被触发时，鼠标指针的水平坐标。</td>
</tr>
<tr>
<td>clientY</td>
<td>返回当事件被触发时，鼠标指针的垂直坐标。</td>
</tr>
<tr>
<td>ctrlKey</td>
<td>返回当事件被触发时，”CTRL” 键是否被按下。</td>
</tr>
<tr>
<td>metaKey</td>
<td>返回当事件被触发时，”meta” 键是否被按下。</td>
</tr>
<tr>
<td>relatedTarget</td>
<td>返回与事件的目标节点相关的节点。</td>
</tr>
<tr>
<td>screenX</td>
<td>返回当某个事件被触发时，鼠标指针的水平坐标。</td>
</tr>
<tr>
<td>screenY</td>
<td>返回当某个事件被触发时，鼠标指针的垂直坐标。</td>
</tr>
<tr>
<td>shiftKey</td>
<td>返回当事件被触发时，”SHIFT” 键是否被按下。</td>
</tr>
</tbody></table>
<h3 id="21-事件高级"><a href="#21-事件高级" class="headerlink" title="21.事件高级"></a>21.事件高级</h3><h4 id="一-事件的绑定"><a href="#一-事件的绑定" class="headerlink" title="一.事件的绑定"></a>一.事件的绑定</h4><blockquote>
<p>给与响应的元素绑定相应的事件。</p>
<p>注册事件有三种方式：</p>
<ul>
<li>传统方式</li>
<li>方法监听注册方式</li>
<li>attachEvent函数添加事件</li>
</ul>
<p>步骤：</p>
<p>​    获取指定元素的对象  –》  给指定元素绑定相应的事件函数</p>
</blockquote>
<h5 id="传统方式绑定事件："><a href="#传统方式绑定事件：" class="headerlink" title="传统方式绑定事件："></a>传统方式绑定事件：</h5><blockquote>
<p>此方法如果给一个元素绑定多个相同的事件，后方的事件会覆盖原来的事件。(==注册事件的唯一性==)</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/*获取指定元素的对象*/</span>
<span class="token keyword">var</span> cbt <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"cql"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*传统方式为指定元素绑定事件函数*/</span>
bt<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
       <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"使用onclick事件绑定的事件！"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="方法监听注册方式"><a href="#方法监听注册方式" class="headerlink" title="方法监听注册方式"></a>方法监听注册方式</h5><blockquote>
<p>如果一个元素绑定事件后，再绑定相同的事件函数，后绑定的会覆盖新绑定的，如果想要继续绑定：</p>
<p>​        元素对象.addEventListener(“绑定事件类型”,function(){},falst);</p>
<p>​        元素对象.addEventListener(“绑定事件类型”,函数名,falst);</p>
<ul>
<li><p>第一个参数： ==绑定的事件的类型，需要以字符串的形式，不需要 加on==;</p>
</li>
<li><p>第二个参数:  绑定的事件函数，可以是具体函数，也可以是函数名；</p>
</li>
<li><p>第三个函数：是否在捕获阶段触发，一般传递falst。(默认冒泡)</p>
<p>可以绑定多个相同的事件函数，并且触发的顺序是按照绑定的先后顺序触发的。</p>
<p>注：<code>ie8及以下不支持</code></p>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/*获取指定元素的对象*/</span>
<span class="token keyword">var</span> bt <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"bt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*为指定元素绑定事件函数*/</span>
            bt<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"使用onclick事件绑定的事件！"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

            <span class="token comment">/*为指定的元素再增加事件函数，想先绑定先执行*/</span>
	bt<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"添加的事件函数1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            bt<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"添加的事件函数2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="不建议使用的事件监听"><a href="#不建议使用的事件监听" class="headerlink" title="不建议使用的事件监听"></a>不建议使用的事件监听</h5><blockquote>
<p>ie8及以下可以使用</p>
<p>​    attachEvent(“事件类型”,function(){});</p>
<p>​    attachEvent(“事件类型”,函数名);</p>
<ul>
<li>第一个参数： ==绑定事件的类型，字符串的形式，需要加on==；</li>
<li>第二个参数：  绑定的事件函数。</li>
<li>先绑定，后执行。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/*获取指定元素的对象*/</span>
<span class="token keyword">var</span> bt <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"cql"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*为指定元素绑定事件函数*/</span>
            bt<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"使用onclick事件绑定的事件！"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
<span class="token comment">/*为ie8及以下绑定多个相同的事件函数*/</span>
		bt<span class="token punctuation">.</span><span class="token function">attachEvent</span><span class="token punctuation">(</span><span class="token string">"onclick"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"ie8及以下绑定事件"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="兼容性事件绑定函数："><a href="#兼容性事件绑定函数：" class="headerlink" title="兼容性事件绑定函数："></a>兼容性事件绑定函数：</h5><blockquote>
<p>大部分浏览器增加事件方法：</p>
<p>​        对象.addEventListener(“事件类型(不加on)”,事件函数，false);</p>
<p>少部分浏览器增加事件方法：</p>
<p>​        对象.attachEvent(“事件类型”,事件函数);</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/*参数一： 绑定事件对象  ，参数二： 绑定事件类型(不加on) ,参数三：函数*/</span>
<span class="token keyword">function</span> <span class="token function">bind</span><span class="token punctuation">(</span><span class="token parameter">obj <span class="token punctuation">,</span> eventStr <span class="token punctuation">,</span> callback</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>addEventListener<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        	<span class="token comment">//大部分浏览器兼容方式</span>
    	obj<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span>evemtStr <span class="token punctuation">,</span> callback <span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
       	      <span class="token comment">//ie8及以下</span>
    	<span class="token comment">//obj.attachEvent("on"+eventStr , callback);</span>
        obj<span class="token punctuation">.</span><span class="token function">attachEvent</span><span class="token punctuation">(</span><span class="token string">"on"</span><span class="token operator">+</span>eventStr <span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//在匿名函数中调用回调函数</span>
            <span class="token function">callback</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span> 
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="二-事件的删除"><a href="#二-事件的删除" class="headerlink" title="二. 事件的删除"></a>二. 事件的删除</h4><blockquote>
<p>可以通过事件的删除，将对应元素已经绑定的事件取消。</p>
<ul>
<li>传统的删除事件</li>
<li>监听器移除事件</li>
<li>detachEvent函数移除事件</li>
</ul>
</blockquote>
<h5 id="传统方式删除事件"><a href="#传统方式删除事件" class="headerlink" title="传统方式删除事件"></a>传统方式删除事件</h5><blockquote>
<p>直接将设置事件的组件的相应事件设为null。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">/*获取指定元素的对象*/</span>
<span class="token keyword">var</span> bt <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"cql"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/*为指定元素绑定事件函数*/</span>
   bt<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"使用onclick事件绑定的事件！"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token comment">/*为指定事件删除事件函数*/</span>
	 bt<span class="token punctuation">.</span>onclick <span class="token operator">=</span> <span class="token keyword">null</span>；<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="监听器移除事件"><a href="#监听器移除事件" class="headerlink" title="监听器移除事件"></a>监听器移除事件</h5><blockquote>
<p>removeEventListener(‘事件’,函数名);</p>
<ul>
<li>第一个参数： ==要删除事件的类型，需要以字符串的形式，不需要 加on==;</li>
<li>第二个参数:  绑定的事件函数。只能是函数名；</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> bt <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"cql"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     bt<span class="token punctuation">.</span>onclick <span class="token operator">=</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
      <span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"第一次触发"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">&#125;</span>
		bt<span class="token punctuation">.</span><span class="token function">removeEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span>fun<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="detachEvent函数移除事件"><a href="#detachEvent函数移除事件" class="headerlink" title="detachEvent函数移除事件"></a>detachEvent函数移除事件</h5><blockquote>
<p>ie8及以下不可以使用</p>
<p>​    detachEvent(“事件类型”,函数名);</p>
<ul>
<li>第一个参数： ==绑定事件的类型，字符串的形式，需要加on==；</li>
<li>第二个参数：  绑定的事件函数,只能是函数名。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> bt <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"cql"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     bt<span class="token punctuation">.</span>onclick <span class="token operator">=</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
      <span class="token keyword">function</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"第一次触发"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">&#125;</span>
		bt<span class="token punctuation">.</span><span class="token function">detachEvent</span><span class="token punctuation">(</span><span class="token string">"onclick"</span><span class="token punctuation">,</span>fun<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h4 id="三-事件的传播"><a href="#三-事件的传播" class="headerlink" title="三. 事件的传播"></a>三. 事件的传播</h4><blockquote>
<p>事件发生时会在元素节点之间按照特定的顺序传播。</p>
<p>事件的传播分为三个阶段：</p>
<ol>
<li>捕获阶段：<ul>
<li>在捕获阶段时从最外层的祖先元素，向目标元素进行事件的捕获，但是默认此时不会触发事件。</li>
</ul>
</li>
<li>目标阶段：<ul>
<li>事件捕获到目标元素，捕获结束开始在目标元素上触发事件。</li>
</ul>
</li>
<li>冒泡阶段：<ul>
<li>事件从目标元素向他的祖先元素传递，依次触发祖先元素上的事件。</li>
</ul>
</li>
</ol>
<ul>
<li>ie8及以下的浏览器没有捕获阶段。</li>
<li>==js代码中只能执行捕获或者冒泡其中的一个阶段。==</li>
<li>onclick和attchEvent只能得到冒泡阶段。</li>
<li>addEventListener()如果第三个参数时true，表示事件捕获阶段调用事件处理程序，如果是false(不写默认就是false)，表示在事件冒泡阶段调用事件处理程序。</li>
<li>onblur，onfocus，onmouseenter,onmouseleave并没有冒泡。</li>
</ul>
</blockquote>
<h4 id="四-冒泡原理"><a href="#四-冒泡原理" class="headerlink" title="四. 冒泡原理"></a>四. 冒泡原理</h4><blockquote>
<p>​    冒泡是指事件的向上传导性，当后代元素上的事件被触发时，其祖先元素的相同事件也会被触发。</p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!--当事件3触发时，其祖先的相同事件，事件2，事件3也会被触发--></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>box1<span class="token punctuation">"</span></span> <span class="token special-attr"><span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token value javascript language-javascript">事件<span class="token number">1</span></span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>box2<span class="token punctuation">"</span></span> <span class="token special-attr"><span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token value javascript language-javascript">事件<span class="token number">2</span></span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
	  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>box3<span class="token punctuation">"</span></span> <span class="token special-attr"><span class="token attr-name">onclick</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token value javascript language-javascript">事件<span class="token number">3</span></span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="五-事件对象"><a href="#五-事件对象" class="headerlink" title="五. 事件对象"></a>五. 事件对象</h4><blockquote>
<p>element.onclick = function(event){}</p>
<ol>
<li>event就是一个事件对象，写到侦听函数的小括号中，当作形参使用。</li>
<li>事件对象只有有了事件才会存在，他是系统自动创建的，不需要传递参数。</li>
<li>事件对象是关于事件的一系列相关数据的集合，与事件相关的一系列信息都存储到这个对象中，有很多的方法。例如：鼠标事件含有鼠标的相关信息：鼠标坐标。键盘事件：判断按下，松开的键。</li>
<li>事件对象的形参可以自由命名。</li>
<li>在ie 8及以前需要使用window.event来使用事件对象。</li>
</ol>
</blockquote>
<table>
<thead>
<tr>
<th>事件对象属性方法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>e.target</td>
<td>返回触发事件的对象 非标准ie6-8</td>
</tr>
<tr>
<td>e.srcElement</td>
<td>返回触发事件的对象</td>
</tr>
<tr>
<td>e.type</td>
<td>返回事件的类型，如click不带on</td>
</tr>
<tr>
<td>e.cancelBubble</td>
<td>该属性阻止冒泡，非标准ie6-8使用</td>
</tr>
<tr>
<td>e.returnValue</td>
<td>该属性阻止默认事件，非标准ie6-8使用</td>
</tr>
<tr>
<td>e.preventDefault()</td>
<td>该方法阻止默认事件，例如：不让链接跳转</td>
</tr>
<tr>
<td>e.stopPropagation()</td>
<td>阻止冒泡</td>
</tr>
</tbody></table>
<ul>
<li>e.target返回的是触发事件的对象，this返回的是绑定事件的对象。(e.target点击了哪个元素，就返回哪个元素，this是哪个元素绑定了这个点击事件就返回哪个元素)</li>
<li>阻止默认行为：让跳转不跳转，或者让提交按钮不提交。默认上下文菜单不出现。</li>
<li>可以使用return: false;来阻止默认行为，并且没有兼容性问题。但是return后面的代码不会被执行，而且只限于传统的注册方式。</li>
</ul>
<h4 id="六-取消冒泡"><a href="#六-取消冒泡" class="headerlink" title="六. 取消冒泡"></a>六. 取消冒泡</h4><blockquote>
<p>​    冒泡大多数情况下对于开发来说都是有益的，但是有些情况下需要取消冒泡。</p>
<p>取消冒泡方法：</p>
<p>​    可以将事件对象的cancelBubble设置为 true； //兼容性不好ie6-8</p>
<p>​        <code>event.cancelBubble = true;</code></p>
<p>​    可以使用e.stopPropagation()取消冒泡</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">box<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

                <span class="token comment">/*去除事件的冒泡，修改event的cancelBubble属性为true*/</span>
                 event <span class="token operator">=</span> event<span class="token operator">||</span>window<span class="token punctuation">.</span>event<span class="token punctuation">;</span>
                 event<span class="token punctuation">.</span>cancelBubble <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
  			<span class="token comment">/* 或者使用stopPropagation()取消当前事件的冒泡。触发事件到此截止*/</span>
				e<span class="token punctuation">.</span><span class="token function">stopPropagation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"这是div的响应事件"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="七-事件的委派"><a href="#七-事件的委派" class="headerlink" title="七. 事件的委派"></a>七. 事件的委派</h4><blockquote>
<p> ​    是将事件统一绑定给元素的共同的祖先元素，这样当后代元素上的事件触发时，会一直冒泡到祖先元素，从而通过祖先元素的响应函数来处理事件。==利用冒泡原理设置每个子节点的事件==</p>
<p>事件委派是利用了冒泡，通过委派可以减少事件绑定的次数，提高程序的性能。<br> 使用e.target属性得到触发事件的元素。</p>
<ul>
<li>但是事件委派父元素中任何位置都有可能触发响应是事件。</li>
<li>事件函数返回的event的target属性表示触发事件的对象，谁触发这个事件，就返回对应标签的对象。</li>
<li>可以使用将期望触发事件的子元素增加特定的class属性值，使用event.target.className是否等于指定的class值来限定触发的范围。</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">box<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>          
         <span class="token comment">/*event.target可以得到当前点击的对象，this得到绑定事件的对象*/</span> 
    	<span class="token keyword">if</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>className <span class="token operator">==</span> <span class="token string">"指定class值"</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            	 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"期望的元素的响应事件"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="八-常用的鼠标事件"><a href="#八-常用的鼠标事件" class="headerlink" title="八.常用的鼠标事件"></a>八.常用的鼠标事件</h4><table>
<thead>
<tr>
<th>鼠标事件</th>
<th>触发条件</th>
</tr>
</thead>
<tbody><tr>
<td>onclick</td>
<td>鼠标点击左键触发</td>
</tr>
<tr>
<td>onmouseover</td>
<td>鼠标经过触发</td>
</tr>
<tr>
<td>onmouseout</td>
<td>鼠标离开触发</td>
</tr>
<tr>
<td>onfocus</td>
<td>获得鼠标焦点触发</td>
</tr>
<tr>
<td>onblur</td>
<td>失去鼠标焦点触发</td>
</tr>
<tr>
<td>onmouseup</td>
<td>鼠标弹起触发</td>
</tr>
<tr>
<td>onmousemove</td>
<td>鼠标移动触发</td>
</tr>
<tr>
<td>onmousedown</td>
<td>鼠标按下触发</td>
</tr>
</tbody></table>
<h5 id="8-1-禁止鼠标右键菜单"><a href="#8-1-禁止鼠标右键菜单" class="headerlink" title="8.1 禁止鼠标右键菜单"></a>8.1 禁止鼠标右键菜单</h5><blockquote>
<p>contextmenu主要控制应该何时显示上下文菜单，主要用于取消默认的上下文菜单。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//为整个文档对象绑定事件，禁止弹出菜单的默认行为。</span>
document<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"contextmenu"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            e<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="8-2-禁止鼠标选中文字"><a href="#8-2-禁止鼠标选中文字" class="headerlink" title="8.2 禁止鼠标选中文字"></a>8.2 禁止鼠标选中文字</h5><blockquote>
<p>selectstart 选择文字事件</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//为整个文档对象绑定事件，禁止选中文字的默认行为。</span>
document<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"selectstart"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         e<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="八-鼠标事件对象"><a href="#八-鼠标事件对象" class="headerlink" title="八* 鼠标事件对象"></a>八* 鼠标事件对象</h4><blockquote>
<p>event对象代表事件的状态，是事件相关的一系列信息的集合。主要使用的是鼠标事件对象：MouseEvent和键盘对象KeyboardEvent。</p>
</blockquote>
<table>
<thead>
<tr>
<th>鼠标事件对象</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>e.clientX</td>
<td>返回鼠标相对于浏览器窗口可视区的X坐标(不加px)</td>
</tr>
<tr>
<td>e.clientY</td>
<td>返回鼠标相对于浏览器窗口可视区的Y坐标(不加px)</td>
</tr>
<tr>
<td>e.pageX</td>
<td>返回鼠标相对于文档页面的X坐标 Ie9+支持(不加px)</td>
</tr>
<tr>
<td>e.pageY</td>
<td>返回鼠标相对于文档页面的Y坐标 ie9+ 支持(不加px)</td>
</tr>
<tr>
<td>e.screenX</td>
<td>返回鼠标相对于电脑屏幕的X坐标(不加px)</td>
</tr>
<tr>
<td>e.screenY</td>
<td>返回鼠标相对于电脑屏幕的Y坐标(不加px)</td>
</tr>
</tbody></table>
<h4 id="九-常用键盘事件"><a href="#九-常用键盘事件" class="headerlink" title="九. 常用键盘事件"></a>九. 常用键盘事件</h4><table>
<thead>
<tr>
<th>键盘事件</th>
<th>触发条件</th>
</tr>
</thead>
<tbody><tr>
<td>onkeyup</td>
<td>某个键盘按键被松开时触发(不区分字母大小写)</td>
</tr>
<tr>
<td>onkeydown</td>
<td>某个键盘按键被按下时触发(不区分字母大小写)</td>
</tr>
<tr>
<td>onkeypress</td>
<td>某个键盘按键被按下时触发(但是不识别功能键，区分字母大小写)</td>
</tr>
</tbody></table>
<h4 id="九-键盘事件对象"><a href="#九-键盘事件对象" class="headerlink" title="九* 键盘事件对象"></a>九* 键盘事件对象</h4><blockquote>
<p>e.keyCode 返回按下键的ASC码值。</p>
<p>onkeydown和onkeyup不区分字母大小写，onkeypress区分字母大小写。更多使用onkeydown和onkeyup可以识别所有键(包括功能键)，Keypress不识别功能键，但是区分大小写，返回不同的ASC码值。</p>
</blockquote>
<h4 id="十-手动调用事件"><a href="#十-手动调用事件" class="headerlink" title="十. 手动调用事件"></a>十. 手动调用事件</h4><blockquote>
<p>可以通过手动使用指定元素的事件。s</p>
<p>元素对象.事件();</p>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">document<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"selectstart"</span><span class="token punctuation">,</span><span class="token function">function</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         e<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
document<span class="token punctuation">.</span><span class="token function">selectstart</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>









<h3 id="22-对象-BOM"><a href="#22-对象-BOM" class="headerlink" title="22.对象(BOM)"></a>22.对象(BOM)</h3><blockquote>
<ul>
<li>浏览器对象模型 ,BOM可以使我们通过js来操作浏览器的操作。</li>
<li>在BOM中提供了一组对象，用来完成对浏览器的操作。</li>
<li>BOM对象：<ul>
<li>Window<ul>
<li>代表中各浏览器的窗口，同时window也是网页中的全局对象。</li>
<li>他是JS访问浏览器窗口的一个接口。</li>
<li>定义在全局作用域中的变量，函数都会变成window对象的属性和方法。</li>
</ul>
</li>
<li>Navigator<ul>
<li>代表当前浏览器的信息。通过该对象可以来识别不同的浏览器。</li>
</ul>
</li>
<li>Loction<ul>
<li>代表当前浏览器的地址栏信息，通过Loction可以获取到地址栏的信息，<code>或者操作浏览器跳转页面</code>。</li>
</ul>
</li>
<li>History<ul>
<li>代表浏览器的历史纪录，可以通过该对象操作浏览器的历史记录。由于隐私原因，该对象不能获取到具体的历史记录，只能操作浏览器向前或者向后翻页。而且该操作只在当此访问时有效。</li>
</ul>
</li>
<li>Screen<ul>
<li>代表用户的屏幕信息 ，通过该对象可以获取用户的显示器的相关信息。</li>
</ul>
</li>
<li>这些BOM对象都是在浏览器中作为Window的属性保存的，可以通过window对象来使用，也可以直接使用。</li>
</ul>
</li>
</ul>
</blockquote>
<h4 id="Navigator"><a href="#Navigator" class="headerlink" title="Navigator"></a>Navigator</h4><blockquote>
<p>代表当前浏览器的信息。通过该对象可以来识别不同的浏览器。也可以得到系统的版本号。</p>
</blockquote>
<h5 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h5><table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>appCodeName</td>
<td>返回浏览器的代码名。</td>
</tr>
<tr>
<td>appMinorVersion</td>
<td>返回浏览器的次级版本。</td>
</tr>
<tr>
<td>appName</td>
<td>返回浏览器的名称。</td>
</tr>
<tr>
<td>appVersion</td>
<td>返回浏览器的平台和版本信息。</td>
</tr>
<tr>
<td>browserLanguage</td>
<td>返回当前浏览器的语言。</td>
</tr>
<tr>
<td>cookieEnabled</td>
<td>返回指明浏览器中是否启用 cookie 的布尔值。</td>
</tr>
<tr>
<td>cpuClass</td>
<td>返回浏览器系统的 CPU 等级。</td>
</tr>
<tr>
<td>onLine</td>
<td>返回指明系统是否处于脱机模式的布尔值。</td>
</tr>
<tr>
<td>platform</td>
<td>返回运行浏览器的操作系统平台。</td>
</tr>
<tr>
<td>systemLanguage</td>
<td></td>
</tr>
<tr>
<td>userAgent</td>
<td>返回由客户机发送服务器的 user-agent 头部的值。</td>
</tr>
<tr>
<td>userLanguage</td>
<td>返回 OS 的自然语言设置。</td>
</tr>
</tbody></table>
<h5 id="userAgent"><a href="#userAgent" class="headerlink" title="userAgent"></a>userAgent</h5><blockquote>
<p>​    由于历史原因，浏览器中的Navigator的属性大多不能使用，但是userAgent可以返回一个字符串，字符串中包含描述浏览器信息的内容。不同的浏览器会有不同的userAgent。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">
<span class="token function">alert</span><span class="token punctuation">(</span>navigator<span class="token punctuation">.</span>userAgent<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//返回值：</span>
Mozilla<span class="token operator">/</span><span class="token number">5.0</span> <span class="token punctuation">(</span>Windows <span class="token constant">NT</span> <span class="token number">10.0</span><span class="token punctuation">;</span> Win64<span class="token punctuation">;</span> x64<span class="token punctuation">)</span> AppleWebKit<span class="token operator">/</span><span class="token number">537.36</span> <span class="token punctuation">(</span><span class="token constant">KHTML</span><span class="token punctuation">,</span> like Gecko<span class="token punctuation">)</span> Chrome<span class="token operator">/</span><span class="token number">90.0</span><span class="token number">.4430</span><span class="token number">.93</span> Safari<span class="token operator">/</span><span class="token number">537.36</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>判断浏览器：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> ua <span class="token operator">=</span> navigator<span class="token punctuation">.</span>userAgent<span class="token punctuation">;</span>

<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">firefox</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>us<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
   		<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"火狐浏览器"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">&#125;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">chrome</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>ua<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
         	<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"谷歌浏览器"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">&#125;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">msie</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>ua<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>    <span class="token comment">//只能判断ie10及以下版本</span>
        	<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"ie浏览器"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
<span class="token comment">/*else if (window.ActiveXObject)&#123;    //ie11做了手脚
                 alart("ie11会判断失误"); 
         &#125;*/</span>
<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">"ActiveXObject"</span> <span class="token keyword">in</span> window<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>    <span class="token comment">//只能判断ie10及以下版本</span>
        	<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"ie浏览器"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>如果通过UserAgent不能判断，可以通过一些浏览器中特有的对象，来判断浏览器的信息。<ul>
<li>比如：ActiveXObject   ie中独有的</li>
</ul>
</li>
</ul>
<h4 id="History"><a href="#History" class="headerlink" title="History"></a>History</h4><blockquote>
<p>​    代表浏览器的历史纪录，可以通过该对象操作浏览器的历史记录。由于隐私原因，该对象不能获取到具体的历史记录，只能操作浏览器向前或者向后翻页。而且该操作只在当此访问时有效。</p>
</blockquote>
<h5 id="属性-1"><a href="#属性-1" class="headerlink" title="属性"></a>属性</h5><blockquote>
<ul>
<li>length       返回浏览器历史列表中url的数量。<code>当次</code>访问的链接的数量。</li>
</ul>
</blockquote>
<h5 id="history-back"><a href="#history-back" class="headerlink" title="history.back()"></a>history.back()</h5><blockquote>
<p>可以用来回退到上一个页面，作用的和浏览器的回退按钮一样。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">evwnt</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                history<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="history-forward"><a href="#history-forward" class="headerlink" title="history.forward()"></a>history.forward()</h5><blockquote>
<p>可以前进到下一个页面，效果 个浏览器的前进按钮一样。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">evwnt</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                history<span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="history-go"><a href="#history-go" class="headerlink" title="history.go();"></a>history.go();</h5><blockquote>
<ul>
<li>可以用来跳转到指定的页面。</li>
<li>他需要一整数作为参数。<ul>
<li>1 ：表示向前跳转一个页面，相当于forward()。</li>
<li>2 ： 表示向前跳转2个页面。</li>
<li>-1 ：表示向后跳转一个页面。</li>
<li>-2 ： 表示享后跳转两个页面。</li>
</ul>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">evwnt</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token comment">//history.back();</span>
                history<span class="token punctuation">.</span><span class="token function">go</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//相当于back();</span>
            <span class="token punctuation">&#125;</span>       
        <span class="token punctuation">&#125;</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h4 id="Location"><a href="#Location" class="headerlink" title="Location"></a>Location</h4><blockquote>
<p>​    代表当前浏览器的地址栏信息，通过Loction可以获取到地址栏的信息，<code>或者操作浏览器跳转页面</code>。</p>
</blockquote>
<blockquote>
<ul>
<li>如果直接打印location,则可以直接获取到地址栏的详细信息(当前页面的完整路径)。</li>
<li>如果将location书香修改为一个完整的路径，或者相对路径，则页面会自动跳转到指定的路径，并且会生成相应的历史记录。</li>
</ul>
</blockquote>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>location.hash</td>
<td>设置或返回从井号 (#) 开始的 URL（锚点）。</td>
</tr>
<tr>
<td>location.host</td>
<td>设置或返回主机名和当前 URL 的端口号。</td>
</tr>
<tr>
<td>location.hostname</td>
<td>设置或返回当前 URL 的主机名。</td>
</tr>
<tr>
<td>location.href</td>
<td>设置或返回完整的 URL。</td>
</tr>
<tr>
<td>location.pathname</td>
<td>设置或返回当前 URL 的路径部分。</td>
</tr>
<tr>
<td>location.port</td>
<td>设置或返回当前 URL 的端口号。</td>
</tr>
<tr>
<td>location.protocol</td>
<td>设置或返回当前 URL 的协议。</td>
</tr>
<tr>
<td>location.search</td>
<td>设置或返回从问号 (?) 开始的 URL（查询部分）。</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>location.assign()</td>
<td>加载新的文档。会生成历史记录,可以后退</td>
</tr>
<tr>
<td>location.reload()</td>
<td>重新加载当前文档。作用和刷新一样</td>
</tr>
<tr>
<td>location.replace()</td>
<td>用新的文档替换当前文档，普通刷新(F5)。不会生成历史记录，无法后退，可以添加参数true表示强制刷新(ctrl + F5)。</td>
</tr>
</tbody></table>
<h4 id="Screen"><a href="#Screen" class="headerlink" title="Screen"></a>Screen</h4><blockquote>
<p>代表用户的屏幕信息 ，通过该对象可以获取用户的显示器的相关信息。</p>
<ul>
<li>返回或设置： 高度宽度，分辨率，刷新率。</li>
</ul>
</blockquote>
<h4 id="window-及事件"><a href="#window-及事件" class="headerlink" title="window 及事件"></a>window 及事件</h4><blockquote>
<p>代表中各浏览器的窗口，同时window也是网页中的全局对象。</p>
</blockquote>
<h5 id="1-window加载事件"><a href="#1-window加载事件" class="headerlink" title="1. window加载事件"></a>1. window加载事件</h5><blockquote>
<ul>
<li>window.onload</li>
<li>addEventListener</li>
</ul>
</blockquote>
<p>window.onload事件</p>
<blockquote>
<p>该事件会在所有网页元素加载后发生，常用与在最上方执行script代码。</p>
<p>会在DOM和样式表，图片，flash等等加载完后执行。</p>
</blockquote>
<p>addEventListener(‘ ‘,function(){},false)</p>
<blockquote>
<p>仅当DOM加载完成，不包括样式表，图片，flash等。</p>
<p>如果页面上的图片很多，从用户访问onload触发可能需求较长的时间，交互</p>
</blockquote>
<h5 id="2-window-调整窗口大小"><a href="#2-window-调整窗口大小" class="headerlink" title="2. window 调整窗口大小"></a>2. window 调整窗口大小</h5><blockquote>
<p>当浏览器窗口大小被改变后会触发一下事件</p>
</blockquote>
<blockquote>
<ul>
<li>window.onresize = function(){}</li>
<li>window.addEventListener(“resize”,function(){});</li>
</ul>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">window<span class="token punctuation">.</span>onload <span class="token operator">=</span> <span class="token function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
     window<span class="token punctuation">.</span>onresize <span class="token operator">=</span> <span class="token function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"高:"</span> <span class="token operator">+</span>  window<span class="token punctuation">.</span>innerWidth<span class="token punctuation">)</span><span class="token punctuation">;</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"低:"</span> <span class="token operator">+</span>  window<span class="token punctuation">.</span>innerHeight<span class="token punctuation">)</span><span class="token punctuation">;</span>
       	
     <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h5 id="3-window-定时器"><a href="#3-window-定时器" class="headerlink" title="3. window 定时器"></a>3. window 定时器</h5><blockquote>
<ul>
<li>setTimeout</li>
<li>setInterval</li>
</ul>
</blockquote>
<h5 id="setInterval"><a href="#setInterval" class="headerlink" title="setInterval()"></a>setInterval()</h5><blockquote>
<ul>
<li><p>重复，可以将一个函数，每隔一段时间执行一次。</p>
</li>
<li><p>参数：</p>
<ul>
<li>回调函数，函数会每个一段时间被调用一次。</li>
<li>每次调用间隔的时间，单位是毫秒。可以省略，省略默认为0.</li>
</ul>
</li>
<li><p>需要定义一个定时器标识，用来设置区分定时器。</p>
<p>语法： </p>
<pre><code>- var 定时器标识 = setInterval(函数,间隔时间);
</code></pre>
</li>
<li><p>如果同时开启多个定时器，被赋值的标识只会标识最新的定时器，但是如果不删除之前开启的定时器，它们还会运行。在开器定时器前关闭之前开启的定时器。</p>
</li>
</ul>
</blockquote>
<h5 id="clearInterval"><a href="#clearInterval" class="headerlink" title="clearInterval()"></a>clearInterval()</h5><blockquote>
<ul>
<li>可以用来关闭一个已经开始的定时器。</li>
<li>方法需要一个定时器的表示作为参数。<ul>
<li>clearInterval(定时器标识);</li>
<li>可以接受任意类型的参数，如果为有效的定时器标识，即为执行，如果不是也不报错。</li>
</ul>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> time <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> count <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"count"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//定义定时器，且指定定时器标识。</span>
            <span class="token keyword">var</span> iv <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                count<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> time<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span>
            btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token comment">//关闭指定的定时器</span>
                <span class="token function">clearInterval</span><span class="token punctuation">(</span>iv<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h5 id="setTimeout"><a href="#setTimeout" class="headerlink" title="setTimeout()"></a>setTimeout()</h5><blockquote>
<ul>
<li><p>延时调用，不会马上执行，而是隔一段时间后再执行，而且只会调用一次。</p>
</li>
<li><p>需要定义一个定时器标识，用来设置区分定时器。</p>
<p>语法： </p>
<ul>
<li>var 定时器标识 = setTimeout(函数,延迟的时间);</li>
</ul>
</li>
</ul>
</blockquote>
<h5 id="clearTimeout"><a href="#clearTimeout" class="headerlink" title="clearTimeout()"></a>clearTimeout()</h5><blockquote>
<ul>
<li>关闭一个延时调用。</li>
<li>方法需要一个定时器的表示作为参数。<ul>
<li>clearTimeout(定时器标识);</li>
</ul>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            
            <span class="token keyword">var</span> count <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"count"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> btn <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"btn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
            <span class="token comment">//设置延迟函数   ---   延迟时间   只会运行一次</span>
           <span class="token keyword">var</span> iv <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

                count<span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">"延迟函数1s显示"</span><span class="token punctuation">;</span>

            <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span>
            btn<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">event</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token comment">//关闭指定的延迟调用</span>
                <span class="token function">clearTimeout</span><span class="token punctuation">(</span>iv<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="23-执行队列"><a href="#23-执行队列" class="headerlink" title="23. 执行队列"></a>23. 执行队列</h3><blockquote>
<p>JavaScript语言的一大特点就是单线程，同一时间只能做一件事，因为这是脚本语言诞生的使命所导致，Javascript是为了处理页面中用户的交互，以及操作DOM而诞生的。</p>
<ol>
<li>先执行执行栈中的同步任务。</li>
<li>异步任务(回调函数)放入任务队列中。</li>
<li>一旦执行栈中的所有同步任务执行完毕，系统就会按次序读取任务队列中的异步任务，于是被读取的异步任务结束等待状态，进入执行栈中，开始执行。</li>
</ol>
</blockquote>
<h4 id="23-1-同步和异步"><a href="#23-1-同步和异步" class="headerlink" title="23.1 同步和异步"></a>23.1 同步和异步</h4><blockquote>
<p>为了解决JS执行时间过长会导致页面渲染不连贯，导致页面渲染加载阻塞的感觉。利用多核CPU的计算能力，HTML5提出WebWorker标准，允许javaScript脚本创建多个线程，JS中出现了同步和异步。</p>
</blockquote>
<ul>
<li>同步：前一个任务结束再执行后一个任务</li>
<li>异步：同时处理多个任务。</li>
</ul>
<p><img src="./img/js%E5%BC%82%E6%AD%A5%E5%A4%84%E7%90%86%E6%9C%BA%E5%88%B6.png"></p>
<p><img src="./img/js%E5%BC%82%E6%AD%A5%E6%89%A7%E8%A1%8C%E6%9C%BA%E5%88%B62.png"></p>
<ul>
<li>会将异步任务交给异步进程处理，如果满足调用的条件，异步进程会将回调函数放到异步任务队列中，等待发送到执行栈中执行。</li>
<li>由于主线程的重复获得任务，执行任务，再获得任务，再执行任务，所以这种机制被称为==事件循环==(event loop).</li>
</ul>
<h5 id="同步任务："><a href="#同步任务：" class="headerlink" title="同步任务："></a>同步任务：</h5><blockquote>
<p>同步任务都在主线程上执行形成一个执行栈。</p>
</blockquote>
<h5 id=""><a href="#" class="headerlink" title=""></a></h5><h5 id="异步任务："><a href="#异步任务：" class="headerlink" title="异步任务："></a>异步任务：</h5><blockquote>
<p>JS的异步任务是通过回调函数来实现的。</p>
<p>异步任务的类型：</p>
<ol>
<li>普通事件： click，resize</li>
<li>资源加载：load，error</li>
<li>定时器：setInterval ，setTimeout</li>
</ol>
</blockquote>
<h3 id="24-PC网页特效"><a href="#24-PC网页特效" class="headerlink" title="24.PC网页特效"></a>24.PC网页特效</h3><h4 id="24-1-元素偏移量-offset"><a href="#24-1-元素偏移量-offset" class="headerlink" title="24.1 元素偏移量 offset"></a>24.1 元素偏移量 offset</h4><blockquote>
<p>offset翻译过来就是偏移量，使用offset系列相关属性可以动态的得到该元素的位置，大小等。</p>
<ul>
<li>获得元素距离带有定位父元素的位置。</li>
<li>获得元素自身的大小(宽度高度)。</li>
<li>==返回的数值不带单位==。</li>
</ul>
</blockquote>
<table>
<thead>
<tr>
<th>offset系列属性</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>element.offsetParent</td>
<td>返回作为该元素带有定位的父级元素，如果父级都没有定位则返回body。</td>
</tr>
<tr>
<td>element.offsetTop</td>
<td>返回元素相对带有定位父元素上方的偏移量，如果没有返回到body的偏移量。</td>
</tr>
<tr>
<td>element.offsetLeft</td>
<td>返回元素相对带有定位父元素左边框的偏移量。</td>
</tr>
<tr>
<td>element.offsetwidth</td>
<td>返回自身包括padding，边框，内容区的宽度，返回数值不带单位。</td>
</tr>
<tr>
<td>element.offsetHeight</td>
<td>返回自身包括padding，边框，内容区的高度，返回数值不带单位。</td>
</tr>
</tbody></table>
<h5 id="style与offset的区别"><a href="#style与offset的区别" class="headerlink" title="style与offset的区别"></a>style与offset的区别</h5><blockquote>
<p>offset:</p>
<ul>
<li>offset可以得到==任意样式表==中的样式值。</li>
<li>offset系列获得的数值是没有单位的==数字==。</li>
<li>offsetWidth包含padding + border + width。</li>
<li>offsetWidth等属性是只读属性，只能获取不能赋值。</li>
<li>获取元素大小位置时使用offset比较合适。</li>
</ul>
<p>style：</p>
<ul>
<li>style只能得到==行内样式表==中的样式值。</li>
<li>style.width获得的时带有单位的==字符串==。</li>
<li>style.width获得不包含padding和border的值。</li>
<li>style.width时可读写属性，可以获取也可以赋值。</li>
<li>给与元素更改值的时候，使用style改变。</li>
</ul>
</blockquote>
<h4 id="24-2-元素可视区-client"><a href="#24-2-元素可视区-client" class="headerlink" title="24.2 元素可视区 client"></a>24.2 元素可视区 client</h4><blockquote>
<p>client(客户端)，使用client系列的相关属性可以获取元素可视区的相关信息，通过clinent系列的相关属性可以动态的获得边框大小，元素大小。</p>
<p>client不计算文字超出的高度，只计算盒子的高度和宽度。</p>
</blockquote>
<table>
<thead>
<tr>
<th>client系列属性</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>element.clientTop</td>
<td>返回元素上边框的大小</td>
</tr>
<tr>
<td>element.clientLeft</td>
<td>返回元素左边框的大小</td>
</tr>
<tr>
<td>element.clientWidth</td>
<td>返回自身包括padding，内容区域宽度，不含边框，返回数值不带单位</td>
</tr>
<tr>
<td>element.clientHeight</td>
<td>返回自身 包括padding，内容区高度，不含边框，返回数值不带单位。</td>
</tr>
</tbody></table>
<h4 id="24-3-元素滚动-scroll"><a href="#24-3-元素滚动-scroll" class="headerlink" title="24.3 元素滚动 scroll"></a>24.3 元素滚动 scroll</h4><blockquote>
<p> scroll 翻译过来是滚动的，使用scroll系列的相关属性可以动态的得到该元素的大小，滚动的距离。</p>
<p> scroll计算文字超出的高度，如果没有超出则会返回盒子的高度。</p>
</blockquote>
<table>
<thead>
<tr>
<th>scroll系列属性</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>element.scrollTop</td>
<td>返回被卷曲的上侧距离，返回数值不带单位</td>
</tr>
<tr>
<td>element.scrollLeft</td>
<td>返回被卷曲的左侧距离，返回数值不带单位</td>
</tr>
<tr>
<td>element.scrollWidth</td>
<td>返回==自身实际==的宽度，不含边框，返回数值不带单位</td>
</tr>
<tr>
<td>element.scrollHeight</td>
<td>返回==自身实际==的高度，不含边框，返回数值不带单位</td>
</tr>
</tbody></table>
<p><img src="./img/scroll%E8%A7%A3%E6%9E%90.png"></p>
<ul>
<li>元素被卷曲的高度宽度使用element.scrollTop与element.scrollLeft属性</li>
<li>页面被卷曲的高度和宽度使用window.pageYOffset与window.pageXOffset来得到</li>
<li>window.scroll(x,y)  设置网页滚动到的位置，不带单位</li>
</ul>
<table>
<thead>
<tr>
<th>三大系列大小对比</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>element.offsetWidth</td>
<td>返回自身包括padding，边框，内容区的宽度，不带单位</td>
</tr>
<tr>
<td>element.clientWidth</td>
<td>返回自身包括padding，，内容区的宽度，不带单位</td>
</tr>
<tr>
<td>element.scrollWidth</td>
<td>返回自身实际的宽度，内容超出后返回实际占有的宽度，不带单位</td>
</tr>
</tbody></table>
<h4 id="24-4-立即执行函数"><a href="#24-4-立即执行函数" class="headerlink" title="24.4 立即执行函数"></a>24.4 立即执行函数</h4><blockquote>
<p>常规函数需要定义之后调用才能执行，立即执行函数不需要调用就可以执行。</p>
<p>立即执行函数中独立创建了一个作用域。里面所有的变量都是局部变量不会有命名冲突的问题。</p>
</blockquote>
<blockquote>
<p>语法：</p>
<p>​    <code>(function(形参)&#123;&#125;)(实参)</code>       //(匿名函数)()</p>
<p>​    <code>(function(形参)&#123;&#125;(实参))</code>    //(匿名函数())</p>
</blockquote>
<h3 id="25-动画函数"><a href="#25-动画函数" class="headerlink" title="25. 动画函数"></a>25. 动画函数</h3><blockquote>
<p>核心原理：通过定时器setInterval()不断移动盒子位置。</p>
<p>实现步骤：</p>
<ul>
<li>获取盒子当前位置。</li>
<li>让盒子在当前位置加上1个移动距离。</li>
</ul>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">window<span class="token punctuation">.</span>onload <span class="token operator">=</span> function <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> div <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//obj动画执行的对象</span>
            <span class="token comment">//target结束时的距离</span>
            function <span class="token function">animate</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> target<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
              
              <span class="token comment">//先去除定时器，防止多个定时器同时执行</span>
              <span class="token function">clearInterval</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token comment">//使用为对象添加属性方式定义定时器</span>
                obj<span class="token punctuation">.</span>time <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span>function <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                  	
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>obj<span class="token punctuation">.</span>offsetLeft <span class="token operator">>=</span> target<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                        <span class="token function">clearInterval</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">&#125;</span>
                    obj<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left <span class="token operator">=</span> obj<span class="token punctuation">.</span>offsetLeft <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token string">"px"</span><span class="token punctuation">;</span>

                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token function">animate</span><span class="token punctuation">(</span>div<span class="token punctuation">,</span><span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="缓动动画"><a href="#缓动动画" class="headerlink" title="缓动动画"></a>缓动动画</h4><blockquote>
<p>原理： </p>
<ol>
<li>盒子每次移动的距离慢慢减小，速度会慢慢下降。</li>
<li>核心算法 ：   每次移动的距离 = （目标值 - 现在位置）/10  </li>
</ol>
</blockquote>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">window<span class="token punctuation">.</span>onload <span class="token operator">=</span> function <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">var</span> div <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"div"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//obj动画执行的对象</span>
            <span class="token comment">//target结束时的距离</span>
            function <span class="token function">animate</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> target<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token comment">//定义缓动的速度</span>
              <span class="token keyword">var</span> step <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
              <span class="token comment">//先去除定时器，方式多个定时器</span>
              <span class="token function">clearInterval</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token comment">//使用为对象添加属性方式定义定时器</span>
                obj<span class="token punctuation">.</span>time <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span>function <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                  	
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>obj<span class="token punctuation">.</span>offsetLeft <span class="token operator">>=</span> target<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                        <span class="token function">clearInterval</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">&#125;</span>
                    step <span class="token operator">=</span> <span class="token punctuation">(</span>target <span class="token operator">-</span> obj<span class="token punctuation">.</span>offsetLeft<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">10</span><span class="token punctuation">;</span> 
                  	step <span class="token operator">></span> <span class="token number">0</span><span class="token operator">?</span> <span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span>step<span class="token punctuation">)</span><span class="token operator">:</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>step<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    obj<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left <span class="token operator">=</span> obj<span class="token punctuation">.</span>offsetLeft <span class="token operator">+</span> step <span class="token operator">+</span> <span class="token string">"px"</span><span class="token punctuation">;</span>

                <span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>
            <span class="token function">animate</span><span class="token punctuation">(</span>div<span class="token punctuation">,</span><span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>







<h3 id="26-本地存储"><a href="#26-本地存储" class="headerlink" title="26. 本地存储"></a>26. 本地存储</h3><blockquote>
<p>随着网页的应用普遍，变得越来越复杂，为了满足存储的要求，html5提出了相关解决办法。</p>
</blockquote>
<blockquote>
<p>数据存储在用户的浏览器中。</p>
<p>设置，读取方便，甚至页面刷新不丢失数据。</p>
<p>容量较大，sessionStorage存储5M数据，localStorage存储20M数据。</p>
<p>只能存储字符串，可以将对下那个json.stringify()编码使用。</p>
</blockquote>
<h4 id="window-sessionStorage"><a href="#window-sessionStorage" class="headerlink" title="window.sessionStorage"></a>window.sessionStorage</h4><blockquote>
<p>声明周期为关闭浏览器窗口，刷新页面不会清除。</p>
<p>在同一窗口(页面)下数据可以共享。</p>
<p>以键值对的形式存储使用。</p>
<p>语法：</p>
<p>​    <code>sessionStorage.setItem(key,value); </code> //设置对应键的值</p>
<p>​    <code>sessionStorage.getItem(key); </code> //获取对应键的值</p>
<p>​    <code>sessionStorage.removeItem(&quot;key&quot;);</code>  //删除指定键的值</p>
<p>​    <code>sessionStorage.clear();</code>    //删除说有的值</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token operator">&lt;</span>body<span class="token operator">></span>
    <span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token string">"#"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"text"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"setb"</span><span class="token operator">></span>存储<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"getb"</span><span class="token operator">></span>获得<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"removeb"</span><span class="token operator">></span>删除<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"delb"</span><span class="token operator">></span>清空<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">></span>

    <span class="token operator">&lt;</span>script<span class="token operator">></span>
        <span class="token keyword">var</span> text <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".text"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> setb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".setb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> getb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".getb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> removeb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".removeb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> delb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".delb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


        setb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//存储到sessionStorage中</span>
            sessionStorage<span class="token punctuation">.</span><span class="token function">setItem</span><span class="token punctuation">(</span><span class="token string">"textkey"</span><span class="token punctuation">,</span>text<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"存储成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        getb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//从sessionStorage中提取指定key的数据</span>
            <span class="token keyword">var</span> str <span class="token operator">=</span> sessionStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">"textkey"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">alert</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        removeb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//从sessionStorage中删除指定key的数据</span>
            <span class="token keyword">var</span> str <span class="token operator">=</span> sessionStorage<span class="token punctuation">.</span><span class="token function">removeItem</span><span class="token punctuation">(</span><span class="token string">"textkey"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        delb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//清空sessionStorage中数据</span>
            sessionStorage<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="windou-localStorage"><a href="#windou-localStorage" class="headerlink" title="windou.localStorage"></a>windou.localStorage</h4><blockquote>
<p>生命周期永久生效，除非手动删除，否则关闭页面也会存在。</p>
<p>可以多窗口(同一浏览器共享)</p>
<p>以键值对的形式存储应用使用。</p>
</blockquote>
<p>语法：</p>
<p>​    <code>localStorage.setItem(key,value); </code> //设置对应键的值</p>
<p>​    <code>localStorage.getItem(key); </code> //获取对应键的值</p>
<p>​    <code>localStorage.removeItem(&quot;key&quot;);</code>  //删除指定键的值</p>
<p>​    <code>localStorage.clear();</code>    //删除说有的值</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token operator">&lt;</span>body<span class="token operator">></span>
    <span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token string">"#"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"text"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"setb"</span><span class="token operator">></span>存储<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"getb"</span><span class="token operator">></span>获得<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"removeb"</span><span class="token operator">></span>删除<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
        <span class="token operator">&lt;</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"delb"</span><span class="token operator">></span>清空<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">></span>

    <span class="token operator">&lt;</span>script<span class="token operator">></span>
        <span class="token keyword">var</span> text <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".text"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> setb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".setb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> getb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".getb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> removeb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".removeb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> delb <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">".delb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


        setb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//存储到localStorage中</span>
            
            localStorage<span class="token punctuation">.</span><span class="token function">setItem</span><span class="token punctuation">(</span><span class="token string">"textkey"</span><span class="token punctuation">,</span>text<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">"存储成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        getb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//从localStorage中提取指定key的数据</span>
            <span class="token keyword">var</span> str <span class="token operator">=</span> localStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">"textkey"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">alert</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        removeb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//从localStorage中删除指定key的数据</span>
            <span class="token keyword">var</span> str <span class="token operator">=</span> localStorage<span class="token punctuation">.</span><span class="token function">removeItem</span><span class="token punctuation">(</span><span class="token string">"textkey"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        delb<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">"click"</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//清空localStorage中数据</span>
            localStorage<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





























































<h3 id="json"><a href="#json" class="headerlink" title="*  json"></a>*  json</h3><blockquote>
<ul>
<li><p>js中的对象只有js自己认识，其他语言不认识。</p>
</li>
<li><p>json就是特殊格式的字符串。这个字符串可以被任意的语言所识别，并且可以转换为任意语言中的对象。</p>
</li>
<li><p><code>json在开发中主要用来数据的交互。</code></p>
</li>
<li><p>json</p>
<ul>
<li>javascript object notation</li>
</ul>
</li>
<li><p>json和js对象的格式一样，只不过json字符串的属性名必须加双引号。</p>
</li>
<li><p>json分类：</p>
<ul>
<li><ol>
<li>对象 {}</li>
<li>数组 []</li>
</ol>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>json中允许的值：</p>
<ul>
<li><ol>
<li>字符串</li>
<li>数值</li>
<li>布尔值</li>
<li>null</li>
<li>对象</li>
<li>数组</li>
</ol>
</li>
</ul>
</li>
<li><p>JSON在ie7及以下不兼容。</p>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token string">'&#123;"name" = "cql","age" = "2"&#125;'</span><span class="token punctuation">;</span>   <span class="token comment">//对象</span>

<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token string">'[1,2,3,4,"hhh"]'</span><span class="token punctuation">;</span>    <span class="token comment">//数组</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="JSON-parse"><a href="#JSON-parse" class="headerlink" title="JSON.parse()"></a>JSON.parse()</h4><blockquote>
<p>可以将json字符串转换为js对象。</p>
<p>需要一个json字符串作为参数，会将该字符串转换为js对象。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> jsonStr <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">"JOSN字符串"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h4 id="JSON-stringify"><a href="#JSON-stringify" class="headerlink" title="JSON.stringify()"></a>JSON.stringify()</h4><blockquote>
<p>可以将一个js对象转换为JSON字符串。</p>
<p>需要一个js对象作为参数，会返回一个JSON字符串。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> jsonStr <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span> js对象 <span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<h4 id="eval"><a href="#eval" class="headerlink" title="eval()"></a>eval()</h4><blockquote>
<ul>
<li><p>可以用来指定一段字符串形式的js代码，并将结构返回。</p>
</li>
<li><p>如果使用eval()执行的字符串中含有{},他会将{}当成是代码块。</p>
<ul>
<li>如果不希望将其当成代码块来执行，就需要在字符串前后各加一个 （） 。</li>
</ul>
</li>
<li><p>少使用，性能较差，且有安全隐患。</p>
</li>
</ul>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">"alert("</span>字符串形式的js代码<span class="token string">");"</span><span class="token punctuation">;</span>

<span class="token function">evel</span><span class="token punctuation">(</span> str <span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//会执行字符串中的js代码</span>

<span class="token function">evel</span><span class="token punctuation">(</span> “（”<span class="token operator">+</span> str <span class="token operator">+</span> “）” <span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//如果要执行的代码中含有&#123;&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h4 id="兼容IE7"><a href="#兼容IE7" class="headerlink" title="兼容IE7"></a>兼容IE7</h4><blockquote>
<p>如果需要兼容ie7及以下浏览器，则可以从外部引入一个js文件来处理。</p>
</blockquote>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>js/json2.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>



<p>json2.js代码：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">//  json2.js</span>
<span class="token comment">//  2016-05-01</span>
<span class="token comment">//  Public Domain.</span>
<span class="token comment">//  NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.</span>
<span class="token comment">//  See http://www.JSON.org/js.html</span>
<span class="token comment">//  This code should be minified before deployment.</span>
<span class="token comment">//  See http://javascript.crockford.com/jsmin.html</span>

<span class="token comment">//  USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO</span>
<span class="token comment">//  NOT CONTROL.</span>

<span class="token comment">//  This file creates a global JSON object containing two methods: stringify</span>
<span class="token comment">//  and parse. This file is provides the ES5 JSON capability to ES3 systems.</span>
<span class="token comment">//  If a project might run on IE8 or earlier, then this file should be included.</span>
<span class="token comment">//  This file does nothing on ES5 systems.</span>

<span class="token comment">//      JSON.stringify(value, replacer, space)</span>
<span class="token comment">//          value       any JavaScript value, usually an object or array.</span>
<span class="token comment">//          replacer    an optional parameter that determines how object</span>
<span class="token comment">//                      values are stringified for objects. It can be a</span>
<span class="token comment">//                      function or an array of strings.</span>
<span class="token comment">//          space       an optional parameter that specifies the indentation</span>
<span class="token comment">//                      of nested structures. If it is omitted, the text will</span>
<span class="token comment">//                      be packed without extra whitespace. If it is a number,</span>
<span class="token comment">//                      it will specify the number of spaces to indent at each</span>
<span class="token comment">//                      level. If it is a string (such as "\t" or "&amp;nbsp;"),</span>
<span class="token comment">//                      it contains the characters used to indent at each level.</span>
<span class="token comment">//          This method produces a JSON text from a JavaScript value.</span>
<span class="token comment">//          When an object value is found, if the object contains a toJSON</span>
<span class="token comment">//          method, its toJSON method will be called and the result will be</span>
<span class="token comment">//          stringified. A toJSON method does not serialize: it returns the</span>
<span class="token comment">//          value represented by the name/value pair that should be serialized,</span>
<span class="token comment">//          or undefined if nothing should be serialized. The toJSON method</span>
<span class="token comment">//          will be passed the key associated with the value, and this will be</span>
<span class="token comment">//          bound to the value.</span>

<span class="token comment">//          For example, this would serialize Dates as ISO strings.</span>

<span class="token comment">//              Date.prototype.toJSON = function (key) &#123;</span>
<span class="token comment">//                  function f(n) &#123;</span>
<span class="token comment">//                      // Format integers to have at least two digits.</span>
<span class="token comment">//                      return (n &lt; 10)</span>
<span class="token comment">//                          ? "0" + n</span>
<span class="token comment">//                          : n;</span>
<span class="token comment">//                  &#125;</span>
<span class="token comment">//                  return this.getUTCFullYear()   + "-" +</span>
<span class="token comment">//                       f(this.getUTCMonth() + 1) + "-" +</span>
<span class="token comment">//                       f(this.getUTCDate())      + "T" +</span>
<span class="token comment">//                       f(this.getUTCHours())     + ":" +</span>
<span class="token comment">//                       f(this.getUTCMinutes())   + ":" +</span>
<span class="token comment">//                       f(this.getUTCSeconds())   + "Z";</span>
<span class="token comment">//              &#125;;</span>

<span class="token comment">//          You can provide an optional replacer method. It will be passed the</span>
<span class="token comment">//          key and value of each member, with this bound to the containing</span>
<span class="token comment">//          object. The value that is returned from your method will be</span>
<span class="token comment">//          serialized. If your method returns undefined, then the member will</span>
<span class="token comment">//          be excluded from the serialization.</span>

<span class="token comment">//          If the replacer parameter is an array of strings, then it will be</span>
<span class="token comment">//          used to select the members to be serialized. It filters the results</span>
<span class="token comment">//          such that only members with keys listed in the replacer array are</span>
<span class="token comment">//          stringified.</span>

<span class="token comment">//          Values that do not have JSON representations, such as undefined or</span>
<span class="token comment">//          functions, will not be serialized. Such values in objects will be</span>
<span class="token comment">//          dropped; in arrays they will be replaced with null. You can use</span>
<span class="token comment">//          a replacer function to replace those with JSON values.</span>

<span class="token comment">//          JSON.stringify(undefined) returns undefined.</span>

<span class="token comment">//          The optional space parameter produces a stringification of the</span>
<span class="token comment">//          value that is filled with line breaks and indentation to make it</span>
<span class="token comment">//          easier to read.</span>

<span class="token comment">//          If the space parameter is a non-empty string, then that string will</span>
<span class="token comment">//          be used for indentation. If the space parameter is a number, then</span>
<span class="token comment">//          the indentation will be that many spaces.</span>

<span class="token comment">//          Example:</span>

<span class="token comment">//          text = JSON.stringify(["e", &#123;pluribus: "unum"&#125;]);</span>
<span class="token comment">//          // text is '["e",&#123;"pluribus":"unum"&#125;]'</span>

<span class="token comment">//          text = JSON.stringify(["e", &#123;pluribus: "unum"&#125;], null, "\t");</span>
<span class="token comment">//          // text is '[\n\t"e",\n\t&#123;\n\t\t"pluribus": "unum"\n\t&#125;\n]'</span>

<span class="token comment">//          text = JSON.stringify([new Date()], function (key, value) &#123;</span>
<span class="token comment">//              return this[key] instanceof Date</span>
<span class="token comment">//                  ? "Date(" + this[key] + ")"</span>
<span class="token comment">//                  : value;</span>
<span class="token comment">//          &#125;);</span>
<span class="token comment">//          // text is '["Date(---current time---)"]'</span>

<span class="token comment">//      JSON.parse(text, reviver)</span>
<span class="token comment">//          This method parses a JSON text to produce an object or array.</span>
<span class="token comment">//          It can throw a SyntaxError exception.</span>

<span class="token comment">//          The optional reviver parameter is a function that can filter and</span>
<span class="token comment">//          transform the results. It receives each of the keys and values,</span>
<span class="token comment">//          and its return value is used instead of the original value.</span>
<span class="token comment">//          If it returns what it received, then the structure is not modified.</span>
<span class="token comment">//          If it returns undefined then the member is deleted.</span>

<span class="token comment">//          Example:</span>

<span class="token comment">//          // Parse the text. Values that look like ISO date strings will</span>
<span class="token comment">//          // be converted to Date objects.</span>

<span class="token comment">//          myData = JSON.parse(text, function (key, value) &#123;</span>
<span class="token comment">//              var a;</span>
<span class="token comment">//              if (typeof value === "string") &#123;</span>
<span class="token comment">//                  a =</span>
<span class="token comment">//   /^(\d&#123;4&#125;)-(\d&#123;2&#125;)-(\d&#123;2&#125;)T(\d&#123;2&#125;):(\d&#123;2&#125;):(\d&#123;2&#125;(?:\.\d*)?)Z$/.exec(value);</span>
<span class="token comment">//                  if (a) &#123;</span>
<span class="token comment">//                      return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],</span>
<span class="token comment">//                          +a[5], +a[6]));</span>
<span class="token comment">//                  &#125;</span>
<span class="token comment">//              &#125;</span>
<span class="token comment">//              return value;</span>
<span class="token comment">//          &#125;);</span>

<span class="token comment">//          myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) &#123;</span>
<span class="token comment">//              var d;</span>
<span class="token comment">//              if (typeof value === "string" &amp;&amp;</span>
<span class="token comment">//                      value.slice(0, 5) === "Date(" &amp;&amp;</span>
<span class="token comment">//                      value.slice(-1) === ")") &#123;</span>
<span class="token comment">//                  d = new Date(value.slice(5, -1));</span>
<span class="token comment">//                  if (d) &#123;</span>
<span class="token comment">//                      return d;</span>
<span class="token comment">//                  &#125;</span>
<span class="token comment">//              &#125;</span>
<span class="token comment">//              return value;</span>
<span class="token comment">//          &#125;);</span>

<span class="token comment">//  This is a reference implementation. You are free to copy, modify, or</span>
<span class="token comment">//  redistribute.</span>

<span class="token comment">/*jslint
    eval, for, this
*/</span>

<span class="token comment">/*property
    JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
    getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
    lastIndex, length, parse, prototype, push, replace, slice, stringify,
    test, toJSON, toString, valueOf
*/</span>


<span class="token comment">// Create a JSON object only if one does not already exist. We create the</span>
<span class="token comment">// methods in a closure to avoid creating global variables.</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token constant">JSON</span> <span class="token operator">!==</span> <span class="token string">"object"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token constant">JSON</span> <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token string">"use strict"</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> rx_one <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^[\],:&#123;&#125;\s]*$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> rx_two <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\\(?:["\\\/bfnrt]|u[0-9a-fA-F]&#123;4&#125;)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> rx_three <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> rx_four <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?:^|:|,)(?:\s*\[)+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> rx_escapable <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> rx_dangerous <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>

    <span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">// Format integers to have at least two digits.</span>
        <span class="token keyword">return</span> n <span class="token operator">&lt;</span> <span class="token number">10</span>
            <span class="token operator">?</span> <span class="token string">"0"</span> <span class="token operator">+</span> n
            <span class="token operator">:</span> n<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">function</span> <span class="token function">this_value</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token class-name">Date</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toJSON <span class="token operator">!==</span> <span class="token string">"function"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

        <span class="token class-name">Date</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">toJSON</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

            <span class="token keyword">return</span> <span class="token function">isFinite</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token operator">?</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUTCFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"-"</span> <span class="token operator">+</span>
                        <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUTCMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"-"</span> <span class="token operator">+</span>
                        <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUTCDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"T"</span> <span class="token operator">+</span>
                        <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUTCHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">":"</span> <span class="token operator">+</span>
                        <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUTCMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">":"</span> <span class="token operator">+</span>
                        <span class="token function">f</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getUTCSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"Z"</span>
                <span class="token operator">:</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>

        <span class="token class-name">Boolean</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toJSON <span class="token operator">=</span> this_value<span class="token punctuation">;</span>
        <span class="token class-name">Number</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toJSON <span class="token operator">=</span> this_value<span class="token punctuation">;</span>
        <span class="token class-name">String</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toJSON <span class="token operator">=</span> this_value<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token keyword">var</span> gap<span class="token punctuation">;</span>
    <span class="token keyword">var</span> indent<span class="token punctuation">;</span>
    <span class="token keyword">var</span> meta<span class="token punctuation">;</span>
    <span class="token keyword">var</span> rep<span class="token punctuation">;</span>


    <span class="token keyword">function</span> <span class="token function">quote</span><span class="token punctuation">(</span><span class="token parameter">string</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// If the string contains no control characters, no quote characters, and no</span>
<span class="token comment">// backslash characters, then we can safely slap some quotes around it.</span>
<span class="token comment">// Otherwise we must also replace the offending characters with safe escape</span>
<span class="token comment">// sequences.</span>

        rx_escapable<span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> rx_escapable<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span>
            <span class="token operator">?</span> <span class="token string">"\""</span> <span class="token operator">+</span> string<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>rx_escapable<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">var</span> c <span class="token operator">=</span> meta<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> <span class="token keyword">typeof</span> c <span class="token operator">===</span> <span class="token string">"string"</span>
                    <span class="token operator">?</span> c
                    <span class="token operator">:</span> <span class="token string">"\\u"</span> <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token string">"0000"</span> <span class="token operator">+</span> a<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\""</span>
            <span class="token operator">:</span> <span class="token string">"\""</span> <span class="token operator">+</span> string <span class="token operator">+</span> <span class="token string">"\""</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>


    <span class="token keyword">function</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> holder</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// Produce a string from holder[key].</span>

        <span class="token keyword">var</span> i<span class="token punctuation">;</span>          <span class="token comment">// The loop counter.</span>
        <span class="token keyword">var</span> k<span class="token punctuation">;</span>          <span class="token comment">// The member key.</span>
        <span class="token keyword">var</span> v<span class="token punctuation">;</span>          <span class="token comment">// The member value.</span>
        <span class="token keyword">var</span> length<span class="token punctuation">;</span>
        <span class="token keyword">var</span> mind <span class="token operator">=</span> gap<span class="token punctuation">;</span>
        <span class="token keyword">var</span> partial<span class="token punctuation">;</span>
        <span class="token keyword">var</span> value <span class="token operator">=</span> holder<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">// If the value has a toJSON method, call it to obtain a replacement value.</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">&amp;&amp;</span> <span class="token keyword">typeof</span> value <span class="token operator">===</span> <span class="token string">"object"</span> <span class="token operator">&amp;&amp;</span>
                <span class="token keyword">typeof</span> value<span class="token punctuation">.</span>toJSON <span class="token operator">===</span> <span class="token string">"function"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            value <span class="token operator">=</span> value<span class="token punctuation">.</span><span class="token function">toJSON</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

<span class="token comment">// If we were called with a replacer function, then call the replacer to</span>
<span class="token comment">// obtain a replacement value.</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> rep <span class="token operator">===</span> <span class="token string">"function"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            value <span class="token operator">=</span> <span class="token function">rep</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>holder<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

<span class="token comment">// What happens next depends on the value's type.</span>

        <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> value<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">case</span> <span class="token string">"string"</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token function">quote</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">case</span> <span class="token string">"number"</span><span class="token operator">:</span>

<span class="token comment">// JSON numbers must be finite. Encode non-finite numbers as null.</span>

            <span class="token keyword">return</span> <span class="token function">isFinite</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
                <span class="token operator">?</span> <span class="token function">String</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
                <span class="token operator">:</span> <span class="token string">"null"</span><span class="token punctuation">;</span>

        <span class="token keyword">case</span> <span class="token string">"boolean"</span><span class="token operator">:</span>
        <span class="token keyword">case</span> <span class="token string">"null"</span><span class="token operator">:</span>

<span class="token comment">// If the value is a boolean or null, convert it to a string. Note:</span>
<span class="token comment">// typeof null does not produce "null". The case is included here in</span>
<span class="token comment">// the remote chance that this gets fixed someday.</span>

            <span class="token keyword">return</span> <span class="token function">String</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// If the type is "object", we might be dealing with an object or an array or</span>
<span class="token comment">// null.</span>

        <span class="token keyword">case</span> <span class="token string">"object"</span><span class="token operator">:</span>

<span class="token comment">// Due to a specification blunder in ECMAScript, typeof null is "object",</span>
<span class="token comment">// so watch out for that case.</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>value<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">return</span> <span class="token string">"null"</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// Make an array to hold the partial results of stringifying this object value.</span>

            gap <span class="token operator">+=</span> indent<span class="token punctuation">;</span>
            partial <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">// Is the value an array?</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">"[object Array]"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// The value is an array. Stringify every element. Use null as a placeholder</span>
<span class="token comment">// for non-JSON values.</span>

                length <span class="token operator">=</span> value<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> length<span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    partial<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> value<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token string">"null"</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>

<span class="token comment">// Join all of the elements together, separated with commas, and wrap them in</span>
<span class="token comment">// brackets.</span>

                v <span class="token operator">=</span> partial<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">0</span>
                    <span class="token operator">?</span> <span class="token string">"[]"</span>
                    <span class="token operator">:</span> gap
                        <span class="token operator">?</span> <span class="token string">"[\n"</span> <span class="token operator">+</span> gap <span class="token operator">+</span> partial<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">",\n"</span> <span class="token operator">+</span> gap<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\n"</span> <span class="token operator">+</span> mind <span class="token operator">+</span> <span class="token string">"]"</span>
                        <span class="token operator">:</span> <span class="token string">"["</span> <span class="token operator">+</span> partial<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"]"</span><span class="token punctuation">;</span>
                gap <span class="token operator">=</span> mind<span class="token punctuation">;</span>
                <span class="token keyword">return</span> v<span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// If the replacer is an array, use it to select the members to be stringified.</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>rep <span class="token operator">&amp;&amp;</span> <span class="token keyword">typeof</span> rep <span class="token operator">===</span> <span class="token string">"object"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                length <span class="token operator">=</span> rep<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> length<span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> rep<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token string">"string"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                        k <span class="token operator">=</span> rep<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
                        v <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>v<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                            partial<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">quote</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>
                                gap
                                    <span class="token operator">?</span> <span class="token string">": "</span>
                                    <span class="token operator">:</span> <span class="token string">":"</span>
                            <span class="token punctuation">)</span> <span class="token operator">+</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">&#125;</span>
                    <span class="token punctuation">&#125;</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// Otherwise, iterate through all of the keys in the object.</span>

                <span class="token keyword">for</span> <span class="token punctuation">(</span>k <span class="token keyword">in</span> value<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                        v <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span>k<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>v<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                            partial<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token function">quote</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>
                                gap
                                    <span class="token operator">?</span> <span class="token string">": "</span>
                                    <span class="token operator">:</span> <span class="token string">":"</span>
                            <span class="token punctuation">)</span> <span class="token operator">+</span> v<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">&#125;</span>
                    <span class="token punctuation">&#125;</span>
                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// Join all of the member texts together, separated with commas,</span>
<span class="token comment">// and wrap them in braces.</span>

            v <span class="token operator">=</span> partial<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">0</span>
                <span class="token operator">?</span> <span class="token string">"&#123;&#125;"</span>
                <span class="token operator">:</span> gap
                    <span class="token operator">?</span> <span class="token string">"&#123;\n"</span> <span class="token operator">+</span> gap <span class="token operator">+</span> partial<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">",\n"</span> <span class="token operator">+</span> gap<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\n"</span> <span class="token operator">+</span> mind <span class="token operator">+</span> <span class="token string">"&#125;"</span>
                    <span class="token operator">:</span> <span class="token string">"&#123;"</span> <span class="token operator">+</span> partial<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"&#125;"</span><span class="token punctuation">;</span>
            gap <span class="token operator">=</span> mind<span class="token punctuation">;</span>
            <span class="token keyword">return</span> v<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>

<span class="token comment">// If the JSON object does not yet have a stringify method, give it one.</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token constant">JSON</span><span class="token punctuation">.</span>stringify <span class="token operator">!==</span> <span class="token string">"function"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        meta <span class="token operator">=</span> <span class="token punctuation">&#123;</span>    <span class="token comment">// table of character substitutions</span>
            <span class="token string-property property">"\b"</span><span class="token operator">:</span> <span class="token string">"\\b"</span><span class="token punctuation">,</span>
            <span class="token string-property property">"\t"</span><span class="token operator">:</span> <span class="token string">"\\t"</span><span class="token punctuation">,</span>
            <span class="token string-property property">"\n"</span><span class="token operator">:</span> <span class="token string">"\\n"</span><span class="token punctuation">,</span>
            <span class="token string-property property">"\f"</span><span class="token operator">:</span> <span class="token string">"\\f"</span><span class="token punctuation">,</span>
            <span class="token string-property property">"\r"</span><span class="token operator">:</span> <span class="token string">"\\r"</span><span class="token punctuation">,</span>
            <span class="token string-property property">"\""</span><span class="token operator">:</span> <span class="token string">"\\\""</span><span class="token punctuation">,</span>
            <span class="token string-property property">"\\"</span><span class="token operator">:</span> <span class="token string">"\\\\"</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
        <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function-variable function">stringify</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> replacer<span class="token punctuation">,</span> space</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// The stringify method takes a value and an optional replacer, and an optional</span>
<span class="token comment">// space parameter, and returns a JSON text. The replacer can be a function</span>
<span class="token comment">// that can replace values, or an array of strings that will select the keys.</span>
<span class="token comment">// A default replacer method can be provided. Use of the space parameter can</span>
<span class="token comment">// produce text that is more easily readable.</span>

            <span class="token keyword">var</span> i<span class="token punctuation">;</span>
            gap <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
            indent <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>

<span class="token comment">// If the space parameter is a number, make an indent string containing that</span>
<span class="token comment">// many spaces.</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> space <span class="token operator">===</span> <span class="token string">"number"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> space<span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    indent <span class="token operator">+=</span> <span class="token string">" "</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>

<span class="token comment">// If the space parameter is a string, it will be used as the indent string.</span>

            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> space <span class="token operator">===</span> <span class="token string">"string"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                indent <span class="token operator">=</span> space<span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// If there is a replacer, it must be a function or an array.</span>
<span class="token comment">// Otherwise, throw an error.</span>

            rep <span class="token operator">=</span> replacer<span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>replacer <span class="token operator">&amp;&amp;</span> <span class="token keyword">typeof</span> replacer <span class="token operator">!==</span> <span class="token string">"function"</span> <span class="token operator">&amp;&amp;</span>
                    <span class="token punctuation">(</span><span class="token keyword">typeof</span> replacer <span class="token operator">!==</span> <span class="token string">"object"</span> <span class="token operator">||</span>
                    <span class="token keyword">typeof</span> replacer<span class="token punctuation">.</span>length <span class="token operator">!==</span> <span class="token string">"number"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">"JSON.stringify"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// Make a fake root object containing our value under the key of "".</span>
<span class="token comment">// Return the result of stringifying the value.</span>

            <span class="token keyword">return</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">,</span> <span class="token punctuation">&#123;</span><span class="token string-property property">""</span><span class="token operator">:</span> value<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>


<span class="token comment">// If the JSON object does not yet have a parse method, give it one.</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token constant">JSON</span><span class="token punctuation">.</span>parse <span class="token operator">!==</span> <span class="token string">"function"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function-variable function">parse</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">text<span class="token punctuation">,</span> reviver</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// The parse method takes a text and an optional reviver function, and returns</span>
<span class="token comment">// a JavaScript value if the text is a valid JSON text.</span>

            <span class="token keyword">var</span> j<span class="token punctuation">;</span>

            <span class="token keyword">function</span> <span class="token function">walk</span><span class="token punctuation">(</span><span class="token parameter">holder<span class="token punctuation">,</span> key</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// The walk method is used to recursively walk the resulting structure so</span>
<span class="token comment">// that modifications can be made.</span>

                <span class="token keyword">var</span> k<span class="token punctuation">;</span>
                <span class="token keyword">var</span> v<span class="token punctuation">;</span>
                <span class="token keyword">var</span> value <span class="token operator">=</span> holder<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">&amp;&amp;</span> <span class="token keyword">typeof</span> value <span class="token operator">===</span> <span class="token string">"object"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    <span class="token keyword">for</span> <span class="token punctuation">(</span>k <span class="token keyword">in</span> value<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                            v <span class="token operator">=</span> <span class="token function">walk</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token keyword">if</span> <span class="token punctuation">(</span>v <span class="token operator">!==</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                                value<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> v<span class="token punctuation">;</span>
                            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
                                <span class="token keyword">delete</span> value<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>
                            <span class="token punctuation">&#125;</span>
                        <span class="token punctuation">&#125;</span>
                    <span class="token punctuation">&#125;</span>
                <span class="token punctuation">&#125;</span>
                <span class="token keyword">return</span> <span class="token function">reviver</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>holder<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>


<span class="token comment">// Parsing happens in four stages. In the first stage, we replace certain</span>
<span class="token comment">// Unicode characters with escape sequences. JavaScript handles many characters</span>
<span class="token comment">// incorrectly, either silently deleting them, or treating them as line endings.</span>

            text <span class="token operator">=</span> <span class="token function">String</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">;</span>
            rx_dangerous<span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>rx_dangerous<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                text <span class="token operator">=</span> text<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>rx_dangerous<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                    <span class="token keyword">return</span> <span class="token string">"\\u"</span> <span class="token operator">+</span>
                            <span class="token punctuation">(</span><span class="token string">"0000"</span> <span class="token operator">+</span> a<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// In the second stage, we run the text against regular expressions that look</span>
<span class="token comment">// for non-JSON patterns. We are especially concerned with "()" and "new"</span>
<span class="token comment">// because they can cause invocation, and "=" because it can cause mutation.</span>
<span class="token comment">// But just to be safe, we want to reject all unexpected forms.</span>

<span class="token comment">// We split the second stage into 4 regexp operations in order to work around</span>
<span class="token comment">// crippling inefficiencies in IE's and Safari's regexp engines. First we</span>
<span class="token comment">// replace the JSON backslash pairs with "@" (a non-JSON character). Second, we</span>
<span class="token comment">// replace all simple value tokens with "]" characters. Third, we delete all</span>
<span class="token comment">// open brackets that follow a colon or comma or that begin the text. Finally,</span>
<span class="token comment">// we look to see that the remaining characters are only whitespace or "]" or</span>
<span class="token comment">// "," or ":" or "&#123;" or "&#125;". If that is so, then the text is safe for eval.</span>

            <span class="token keyword">if</span> <span class="token punctuation">(</span>
                rx_one<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>
                    text
                        <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>rx_two<span class="token punctuation">,</span> <span class="token string">"@"</span><span class="token punctuation">)</span>
                        <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>rx_three<span class="token punctuation">,</span> <span class="token string">"]"</span><span class="token punctuation">)</span>
                        <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>rx_four<span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span>
                <span class="token punctuation">)</span>
            <span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>

<span class="token comment">// In the third stage we use the eval function to compile the text into a</span>
<span class="token comment">// JavaScript structure. The "&#123;" operator is subject to a syntactic ambiguity</span>
<span class="token comment">// in JavaScript: it can begin a block or an object literal. We wrap the text</span>
<span class="token comment">// in parens to eliminate the ambiguity.</span>

                j <span class="token operator">=</span> <span class="token function">eval</span><span class="token punctuation">(</span><span class="token string">"("</span> <span class="token operator">+</span> text <span class="token operator">+</span> <span class="token string">")"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// In the optional fourth stage, we recursively walk the new structure, passing</span>
<span class="token comment">// each name/value pair to a reviver function for possible transformation.</span>

                <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> reviver <span class="token operator">===</span> <span class="token string">"function"</span><span class="token punctuation">)</span>
                    <span class="token operator">?</span> <span class="token function">walk</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span><span class="token string-property property">""</span><span class="token operator">:</span> j<span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span>
                    <span class="token operator">:</span> j<span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

<span class="token comment">// If the text is not JSON parseable, then a SyntaxError is thrown.</span>

            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">SyntaxError</span><span class="token punctuation">(</span><span class="token string">"JSON.parse"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h2 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h2><h3 id="getStyle"><a href="#getStyle" class="headerlink" title="getStyle"></a>getStyle</h3><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">
<span class="token comment">//获取指定元素的样式</span>
<span class="token comment">// obj： 要获取样式的元素</span>
<span class="token comment">// name: 要获取样式的名字</span>

<span class="token keyword">function</span> <span class="token function">getStyle</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span>name</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>window<span class="token punctuation">.</span>getComputedStyle<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//正常的浏览器的方式，具有getComputedStyle()方法</span>
        <span class="token keyword">return</span> <span class="token function">getComputedStyle</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">else</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//IE8的方式，没有getComputedStyle()方法</span>
        <span class="token keyword">return</span> obj<span class="token punctuation">.</span>currentStyle<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>





<h3 id="move"><a href="#move" class="headerlink" title="move"></a>move</h3><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">
<span class="token comment">//获取指定元素的样式</span>
<span class="token comment">// obj： 要获取样式的元素</span>
<span class="token comment">// name: 要获取样式的名字</span>

<span class="token keyword">function</span> <span class="token function">getStyle</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span>name</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>window<span class="token punctuation">.</span>getComputedStyle<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//正常的浏览器的方式，具有getComputedStyle()方法</span>
        <span class="token keyword">return</span> <span class="token function">getComputedStyle</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">else</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//IE8的方式，没有getComputedStyle()方法</span>
        <span class="token keyword">return</span> obj<span class="token punctuation">.</span>currentStyle<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>


            <span class="token comment">// 参数： </span>
            <span class="token comment">// obj：要执行动画的对象</span>
            <span class="token comment">// attr：要执行动画的样式，比如： left top width height </span>
            <span class="token comment">// target: 执行动画的目标位置</span>
            <span class="token comment">// speed：移动的速度(整数右，负数左)</span>
            <span class="token comment">// callback：回调函数(会在动画执行完毕后执行)</span>

<span class="token keyword">function</span> <span class="token function">move</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span>attr<span class="token punctuation">,</span>target<span class="token punctuation">,</span>speed<span class="token punctuation">,</span>callback</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
    <span class="token comment">//关闭上一个定时器</span>
    <span class="token function">clearInterval</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//获取元素的位置</span>
    <span class="token keyword">var</span> current <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token function">getStyle</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>attr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//判断速度的正负</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>current <span class="token operator">></span> target<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        speed <span class="token operator">=</span> <span class="token operator">-</span>speed
    <span class="token punctuation">&#125;</span>
    obj<span class="token punctuation">.</span>timer <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//获取原来的style值</span>
        <span class="token keyword">var</span> oldValue <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token function">getStyle</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>attr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//在旧的值上增加</span>
        <span class="token keyword">var</span> newValue <span class="token operator">=</span> oldValue <span class="token operator">+</span> speed<span class="token punctuation">;</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>speed <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> newValue <span class="token operator">&lt;</span> target<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">(</span>speed <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> newValue <span class="token operator">></span>target<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            newValue <span class="token operator">=</span> target<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        <span class="token comment">//设置新的值</span>
        obj<span class="token punctuation">.</span>style<span class="token punctuation">[</span>attr<span class="token punctuation">]</span> <span class="token operator">=</span> newValue <span class="token operator">+</span> <span class="token string">"px"</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span>newValue <span class="token operator">==</span> target<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token comment">//达到目标，关闭定时器</span>
            <span class="token function">clearInterval</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//执行完毕调用函数</span>
            callback <span class="token operator">&amp;&amp;</span> <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="轮播图"><a href="#轮播图" class="headerlink" title="轮播图"></a>轮播图</h3><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token constant">DOCTYPE</span> html<span class="token operator">></span>
<span class="token operator">&lt;</span>html lang<span class="token operator">=</span><span class="token string">"en"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>head<span class="token operator">></span>
    <span class="token operator">&lt;</span>meta charset<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>meta http<span class="token operator">-</span>equiv<span class="token operator">=</span><span class="token string">"X-UA-Compatible"</span> content<span class="token operator">=</span><span class="token string">"IE=edge"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>meta name<span class="token operator">=</span><span class="token string">"viewport"</span> content<span class="token operator">=</span><span class="token string">"width=device-width, initial-scale=1.0"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>title<span class="token operator">></span>Document<span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">></span>

    <span class="token operator">&lt;</span>style<span class="token operator">></span>
        <span class="token operator">*</span> <span class="token punctuation">&#123;</span>
            <span class="token literal-property property">margin</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token literal-property property">padding</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
        img <span class="token punctuation">&#123;</span>
            <span class="token literal-property property">width</span><span class="token operator">:</span> 400px<span class="token punctuation">;</span>
            <span class="token literal-property property">height</span><span class="token operator">:</span> 200px<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        #outer <span class="token punctuation">&#123;</span>
            <span class="token literal-property property">position</span><span class="token operator">:</span> relative<span class="token punctuation">;</span>
            <span class="token literal-property property">width</span><span class="token operator">:</span> 410px<span class="token punctuation">;</span>
            <span class="token literal-property property">height</span><span class="token operator">:</span> 200px<span class="token punctuation">;</span>
            <span class="token literal-property property">margin</span><span class="token operator">:</span> 200px auto<span class="token punctuation">;</span>
            <span class="token literal-property property">border</span><span class="token operator">:</span> solid 2px <span class="token function">rgb</span><span class="token punctuation">(</span><span class="token number">126</span><span class="token punctuation">,</span> <span class="token number">219</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            background<span class="token operator">-</span>color<span class="token operator">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span><span class="token number">205</span><span class="token punctuation">,</span> <span class="token number">240</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
            <span class="token literal-property property">padding</span><span class="token operator">:</span> 2px 0px<span class="token punctuation">;</span>
            <span class="token literal-property property">overflow</span><span class="token operator">:</span> hidden<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        #imgList <span class="token punctuation">&#123;</span>
            <span class="token comment">/* width: 2050px; */</span>
            list<span class="token operator">-</span>style<span class="token operator">:</span> none<span class="token punctuation">;</span>
            <span class="token literal-property property">position</span><span class="token operator">:</span> absolute<span class="token punctuation">;</span>
            <span class="token literal-property property">left</span><span class="token operator">:</span> 0px<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        #imgList li <span class="token punctuation">&#123;</span>
            <span class="token literal-property property">float</span><span class="token operator">:</span> left<span class="token punctuation">;</span> 
            <span class="token literal-property property">margin</span><span class="token operator">:</span> <span class="token number">0</span> 5px<span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        #navDiv <span class="token punctuation">&#123;</span>
            <span class="token literal-property property">position</span><span class="token operator">:</span> absolute<span class="token punctuation">;</span>
            <span class="token literal-property property">top</span><span class="token operator">:</span> 170px<span class="token punctuation">;</span>
            <span class="token comment">/* left: 110px; */</span>
        <span class="token punctuation">&#125;</span>

        #navDiv a<span class="token punctuation">&#123;</span>
            <span class="token literal-property property">float</span><span class="token operator">:</span> left<span class="token punctuation">;</span>
            <span class="token literal-property property">width</span><span class="token operator">:</span> 15px<span class="token punctuation">;</span>
            <span class="token literal-property property">height</span><span class="token operator">:</span> 15px<span class="token punctuation">;</span>
            background<span class="token operator">-</span>color<span class="token operator">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">197</span><span class="token punctuation">,</span> <span class="token number">211</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token literal-property property">margin</span><span class="token operator">:</span> <span class="token number">0</span> 10px<span class="token punctuation">;</span>
            border<span class="token operator">-</span>radius<span class="token operator">:</span> 2px<span class="token punctuation">;</span>
            <span class="token comment">/* 设置半透明的程度 */</span>
            <span class="token literal-property property">opacity</span><span class="token operator">:</span> <span class="token number">0.3</span><span class="token punctuation">;</span>
            <span class="token literal-property property">filter</span><span class="token operator">:</span> <span class="token function">alpha</span><span class="token punctuation">(</span>opacity<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>

        #navDiv a<span class="token operator">:</span>hover <span class="token punctuation">&#123;</span>
            <span class="token literal-property property">opacity</span><span class="token operator">:</span> <span class="token number">0.7</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>style<span class="token operator">></span>
        <span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span> src<span class="token operator">=</span><span class="token string">"js/move.js"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span>
        <span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span><span class="token operator">></span>
            window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                <span class="token comment">//设置定时器名(自动跳转的定时器)</span>
                <span class="token keyword">var</span> timer<span class="token punctuation">;</span>
                
                <span class="token comment">//设置图片容器的宽度</span>
                <span class="token keyword">var</span> imgList <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"imgList"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">var</span> imgArr <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">"img"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">// 动态调整图片列表盒子的宽度</span>
                imgList<span class="token punctuation">.</span>style<span class="token punctuation">.</span>width <span class="token operator">=</span> <span class="token number">410</span> <span class="token operator">*</span> imgArr<span class="token punctuation">.</span>length <span class="token operator">+</span> <span class="token string">"px"</span><span class="token punctuation">;</span>

                <span class="token comment">//设置导航按钮居中</span>
                <span class="token keyword">var</span> navDiv <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"navDiv"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">var</span> outer <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">"outer"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                navDiv<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token punctuation">(</span>outer<span class="token punctuation">.</span>offsetWidth <span class="token operator">-</span> navDiv<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span> <span class="token operator">+</span> <span class="token string">"px"</span><span class="token punctuation">;</span>

                <span class="token comment">//图片的索引</span>
                <span class="token keyword">var</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
                
                <span class="token comment">//获取所有的a</span>
                <span class="token keyword">var</span> allA <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                allA<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">.</span>style<span class="token punctuation">.</span>opacity <span class="token operator">=</span> <span class="token string">"0.7"</span><span class="token punctuation">;</span>

                <span class="token comment">//为所有超链接绑定事件</span>
                <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>allA<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

                    <span class="token comment">//每个超链接保存自己的序号</span>
                    allA<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>num <span class="token operator">=</span> i<span class="token punctuation">;</span>

                    allA<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>


                        <span class="token comment">// 关闭定时器，以防止冲突</span>
                        <span class="token function">clearInterval</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        
                        index <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>num<span class="token punctuation">;</span>
                        <span class="token comment">// imgList.style.left = (index * -410) + "px";</span>
                        <span class="token function">move</span><span class="token punctuation">(</span>imgList<span class="token punctuation">,</span><span class="token string">"left"</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">410</span><span class="token operator">*</span>index<span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                            <span class="token function">setA</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token function">autoChange</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">&#125;</span>
                <span class="token punctuation">&#125;</span>

                <span class="token comment">//开启自动轮播</span>
                <span class="token function">autoChange</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>



                <span class="token comment">//设置所有的a背景透明度返回默认值</span>
                <span class="token keyword">function</span> <span class="token function">setA</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                    <span class="token comment">//判断是否为最后一张图片，如果是则设为一</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span>index <span class="token operator">>=</span> imgArr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                        index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

                        <span class="token comment">//设置跳转到第一张</span>
                        imgList<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span>
                    <span class="token punctuation">&#125;</span>

                    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>allA<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                        <span class="token comment">//设置内联样式为空串，不会影响到hove</span>
                        allA<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>style<span class="token punctuation">.</span>opacity <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
                    <span class="token punctuation">&#125;</span>
                    <span class="token comment">//清除a的变化后用以下语句重新设置特别的样式</span>
                        allA<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">.</span>style<span class="token punctuation">.</span>opacity <span class="token operator">=</span> <span class="token string">"0.7"</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>


                <span class="token comment">//自动切换函数</span>


                <span class="token keyword">function</span> <span class="token function">autoChange</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

                timer <span class="token operator">=</span>  <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                        <span class="token comment">//使索引自增，以索引lai控制超链接和移动距离</span>
                        index<span class="token operator">++</span><span class="token punctuation">;</span>
                        <span class="token comment">// if(index > 4)&#123;</span>
                        <span class="token comment">//     index = 0;</span>
                        <span class="token comment">// &#125;</span>

                        index <span class="token operator">%=</span> imgArr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
                        
                        <span class="token function">move</span><span class="token punctuation">(</span>imgList<span class="token punctuation">,</span><span class="token string">"left"</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">410</span><span class="token operator">*</span>index<span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                             <span class="token comment">//setA()会清除所有a的变化</span>
                            <span class="token function">setA</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
                        
                    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token punctuation">&#125;</span>
            <span class="token punctuation">&#125;</span>
        <span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">></span>
<span class="token operator">&lt;</span>body<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 用于放置无序列表大的容器 <span class="token operator">--</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>div id<span class="token operator">=</span><span class="token string">"outer"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 用于放置图片 <span class="token operator">--</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>ul id<span class="token operator">=</span><span class="token string">"imgList"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string">"images/sister.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
            <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string">"images/dao.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
            <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string">"images/fear.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
            <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string">"images/gz.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
            <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string">"images/san.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 最后一张图片和第一张相同 <span class="token operator">--</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string">"images/sister.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">/</span>ul<span class="token operator">></span>
        <span class="token operator">&lt;</span>div id<span class="token operator">=</span><span class="token string">"navDiv"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 超链接中href设置为javascript<span class="token operator">:</span><span class="token punctuation">;</span>可以取消超链接的默认跳转行为 <span class="token operator">--</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string">"javascript:;"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">></span>
            <span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string">"javascript:;"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">></span>
            <span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string">"javascript:;"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">></span>
            <span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string">"javascript:;"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">></span>
            <span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string">"javascript:;"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>






                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        Author:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">凡诚</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        Link:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://example.com/2021/11/09/javascript-ji-chu/">http://example.com/2021/11/09/javascript-ji-chu/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        Reprint policy:
                    </i>
                </span>
                <span class="reprint-info">
                    All articles in this blog are used except for special statements
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    reprint polocy. If reproduced, please indicate source
                    <a href="/about" target="_blank">凡诚</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>Copied successfully, please follow the reprint policy of this article</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">more</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/html/">
                                    <span class="chip bg-color">html</span>
                                </a>
                            
                                <a href="/tags/javaScript/">
                                    <span class="chip bg-color">javaScript</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC81NzU4NC8zNDA0OA==">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;Previous</div>
            <div class="card">
                <a href="/2021/11/19/java8/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/8.jpg" class="responsive-img" alt="java8基础">
                        
                        <span class="card-title">java8基础</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            java是一门面向对象的编程语言，不仅吸收了C++语言的各种优点，还摒弃了C++里难以理解的多继承、指针等概念，因此Java语言具有功能强大和简单易用两个特征。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-11-19
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%90%8E%E7%AB%AF%E6%8A%80%E6%9C%AF/" class="post-category">
                                    后端技术
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/java/">
                        <span class="chip bg-color">java</span>
                    </a>
                    
                    <a href="/tags/java8/">
                        <span class="chip bg-color">java8</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                Next&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/11/08/mrakdown-yu-yan-jian-jie/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/1.jpg" class="responsive-img" alt="mrakdown语言简介">
                        
                        <span class="card-title">mrakdown语言简介</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Markdown 是一种轻量级标记语言，它允许人们使用易读易写的纯文本格式编写文档。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-11-08
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Markdown/" class="post-category">
                                    Markdown
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Typora/">
                        <span class="chip bg-color">Typora</span>
                    </a>
                    
                    <a href="/tags/Markdown/">
                        <span class="chip bg-color">Markdown</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;TOC</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        font-size: 15px;
        color: blue;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2477330090"
                   fixed='true'
                   autoplay='false'
                   theme='blue'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2022</span>
            
            <span id="year">2022</span>
            <a href="/about" target="_blank">凡诚</a>
            &nbsp;&nbsp;<a href="https://hexo.io/" target="_blank"></a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">607.1k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2022";
                    var startMonth = "11";
                    var startDate = "7";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/fanshicheng" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2639144944@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2639144944" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2639144944" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;Search</span>
            <input type="search" id="searchInput" name="s" placeholder="Please enter a search keyword"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <script type="text/javascript">
        var OriginTitile=document.title,st;
        document.addEventListener("visibilitychange",function(){
            document.hidden?(document.title="(Ő∀Ő3)ノ",clearTimeout(st)):(document.title="ヽ(●-`Д´-)ノ欢迎回来",st=setTimeout(function(){document.title=OriginTitile},3e3))
        })
    </script>
    <!-- <script src="/js/cursor.js"></script> -->
    <script type="text/javascript">
        //只在桌面版网页启用特效
        var windowWidth = $(window).width();
        if (windowWidth > 768) {
            document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
        }
        </script>
    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    
    <script>
        (function (i, s, o, g, r, a, m) {
            i["DaoVoiceObject"] = r;
            i[r] = i[r] || function () {
                (i[r].q = i[r].q || []).push(arguments)
            }, i[r].l = 1 * new Date();
            a = s.createElement(o), m = s.getElementsByTagName(o)[0];
            a.async = 1;
            a.src = g;
            a.charset = "utf-8";
            m.parentNode.insertBefore(a, m)
        })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
            "//widget.daovoice.io/widget/6984b559.js", "daovoice")
        daovoice('init', {
            app_id: "377cb7de"
        });
        daovoice('update');
    </script>
    

	
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/libs/background/canvas-nest.js"></script>
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
    <!-- 冒泡 -->
    
<canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
<script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
<script type="text/javascript" src="/js/fireworks.js"></script>


</body>

</html>
